【发布时间】:2023-04-07 22:45:01
【问题描述】:
我想将位图图像保存在 sd 卡中,我可以保存它,但有时我的活动因内存不足而被杀死。
所以我可以将图像保存在块中,而不是以字节数组的形式保存。
我的代码如下:
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg");
if (f.exists()) {
f.delete();
}
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
【问题讨论】:
-
是否显示任何错误,如果是,请发布您的 logcat。
-
没有 Grishu,它只会杀死活动并降低活动堆栈中的活动。我已经在具有更多内存的设备中尝试过这个,它工作正常,所以我得出结论,这是内存问题。
-
查看我的答案并尝试使用它,确保它会对您有所帮助。
-
我已经对图像进行了缩放。 :(
-
查看我的更新答案。
标签: android android-sdcard save-image