gpt4 book ai didi

android - 将多个图像保存到外部存储android

转载 作者:行者123 更新时间:2023-11-30 03:35:25 25 4
gpt4 key购买 nike

我的 res/drawable 文件夹中有 50 多张图片。我想将这些图像保存到外部存储器,然后在 ImageView /图像切换器中一张一张地显示这些图像。我使用下面的代码将单个图像保存到外部存储。但我无法弄清楚如何将所有这些图像一起(一次)保存到外部存储器。

public void SaveImage(){
if (!CheckExternalStorage()) {
return;
}

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a01);
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(path, "image1.png");
file.createNewFile();
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
Log.i(LOGTAG, "Image Written to Exterbal Storage");

} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}


}

最佳答案

使用来自:https://stackoverflow.com/a/3221787/794088 的答案进行一些修改以使用参数调用您的方法 SaveImage

...
R.drawable drawableResources = new R.drawable();
Class<R.drawable> c = R.drawable.class;
Field[] fields = c.getDeclaredFields();

for (int i = 0, max = fields.length; i < max; i++) {
final int resourceId;
try {
resourceId = fields[i].getInt(drawableResources);
// call save with param of resourceId
SaveImage(resourceId);
} catch (Exception e) {
continue;
}
}

...

public void SaveImage(int resId){
if (!CheckExternalStorage()) {
return;
}

Bitmap bmp = BitmapFactory.decodeResource(getResources(), resID);
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(path, "image1.png");
file.createNewFile();
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
Log.i(LOGTAG, "Image Written to Exterbal Storage");

} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}


}

关于android - 将多个图像保存到外部存储android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16699462/

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