简体   繁体   English

Android 位图保存错误。 保存图像错误,将图像保存到 SD 卡后更新图库

[英]Android bitmap saving error. saving image is wrong , update gallery after saving image to sd card

I created a function to save a selected image from gallery.I realize that when the image is saved for the first time it is ok.But if the process start again, it seems that the image is saved (with the same name) but the image don't change in the explorer.It just keep the old bitmap image.I tested on two different devices but the same result.Actually on an Infinix hot 7 but same result.我创建了一个函数来保存从图库中选择的图像。我意识到第一次保存图像时可以。但是如果再次启动该过程,似乎图像已保存(具有相同的名称)但图像在资源管理器中不会改变。它只是保留旧的位图图像。我在两个不同的设备上进行了测试,但结果相同。实际上,在 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;
}

绿树形象

红树形象

You can see that i chose a photo of green nature but it registers an old red tree photo你可以看到我选择了一张绿色自然的照片,但它注册了一张老红树照片

将图像保存在图库中后,您应该将图像更新到图库和文件浏览器的系统

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM