我创建了一个函数来保存从图库中选择的图像。我意识到第一次保存图像时没问题。但是如果再次启动该过程,似乎图像已保存(具有相同的名称) )但图像在资源管理器中不会改变。它只是保留旧的位图图像。我在两个不同的设备上进行了测试,但结果相同。实际上在 Infinix hot 7 上测试,但结果相同。
public static String saveImage(Bitmap bitmap, File[] folders, File file, Context context) {
for (File folder:folders) {
if (!folder.exists()) {
folder.mkdirs();
}
}
if (file.exists ()) {
file.delete();
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
你可以看到我选择了一张绿色自然的照片,但它记录了一张古老的红树照片
最佳答案
将图像保存到图库中后,您应该将图像更新到图库和文件资源管理器的系统
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));
关于java - Android 位图保存错误。保存图片错误,将图片保存到sd卡后更新图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56333666/