简体   繁体   中英

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.

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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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