gpt4 book ai didi

android - 将多张图片保存到 SD 卡

转载 作者:行者123 更新时间:2023-11-29 15:57:16 26 4
gpt4 key购买 nike

我的项目有 30 张可绘制图像,我想通过单击按钮将所有这些图像保存/复制到 SD 卡。我正在使用下面的代码将图像保存到 SD 卡,但我不想复制粘贴此代码 30 次以保存所有图像。那么对于这个问题有没有更好的解决办法。谢谢

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aurora);
String fileName = "aurora.png";

File sd = Environment.getExternalStorageDirectory();
File folder = new File(sd + "/Wallpaper Pack");
folder.mkdir();

File dest = new File(folder, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

很简单,创建一个数组并循环它。

int[] drawablesArr = {R.id.name1, R.id.name2, ....}

for(int i=0l i<=drawablesArr.length; i++){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawablesArr[i]);
String fileName = "image_"+ String.valueOf(i)+".png" ;

File sd = Environment.getExternalStorageDirectory();
File folder = new File(sd + "/Wallpaper Pack");
folder.mkdir();

File dest = new File(folder, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于android - 将多张图片保存到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27320954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com