java - 保存的(位图)图像需要一段时间才能出现在图库中

标签 java android bitmap imageview gallery

我有一个应用程序,可以对位图进行一些编辑,然后将它们保存到媒体存储中。它已成功保存到文件浏览器,但需要一段时间才能出现在图库中(长达数小时)或直到我重新启动设备。

注意:图像会立即保存到文件浏览器中,一切正常

这是我的图像保护程序类:

public class ImageSaver {


public static final String insertImage(ContentResolver cr,
                                       Bitmap source,
                                       String title,
                                       String description) {

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, title);
    values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
    values.put(MediaStore.Images.Media.DESCRIPTION, description);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    // Add the date meta data to ensure the image is added at the front of the gallery
    values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());

    Uri url = null;
    String stringUrl = null;    /* value to be returned */

    try {
        url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        if (source != null) {
            OutputStream imageOut = cr.openOutputStream(url);
            try {
                source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
            } finally {
                imageOut.close();
            }

            long id = ContentUris.parseId(url);
            // Wait until MINI_KIND thumbnail is generated.
            Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
            // This is for backward compatibility.
            storeThumbnail(cr, miniThumb, id, 50F, 50F, MediaStore.Images.Thumbnails.MICRO_KIND);
        } else {
            cr.delete(url, null, null);
            url = null;
        }
    } catch (Exception e) {
        if (url != null) {
            cr.delete(url, null, null);
            url = null;
        }
    }

    if (url != null) {
        stringUrl = url.toString();
    }

    return stringUrl;
}

/**
 * A copy of the Android internals StoreThumbnail method, it used with the insertImage to
 * populate the android.provider.MediaStore.Images.Media#insertImage with all the correct
 * meta data. The StoreThumbnail method is private so it must be duplicated here.
 * @see android.provider.MediaStore.Images.Media (StoreThumbnail private method)
 */
private static final Bitmap storeThumbnail(
        ContentResolver cr,
        Bitmap source,
        long id,
        float width,
        float height,
        int kind) {

    // create the matrix to scale it
    Matrix matrix = new Matrix();

    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();

    matrix.setScale(scaleX, scaleY);

    Bitmap thumb = Bitmap.createBitmap(source, 0, 0,
            source.getWidth(),
            source.getHeight(), matrix,
            true
    );

    ContentValues values = new ContentValues(4);
    values.put(MediaStore.Images.Thumbnails.KIND,kind);
    values.put(MediaStore.Images.Thumbnails.IMAGE_ID,(int)id);
    values.put(MediaStore.Images.Thumbnails.HEIGHT,thumb.getHeight());
    values.put(MediaStore.Images.Thumbnails.WIDTH,thumb.getWidth());

    Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);

    try {
        OutputStream thumbOut = cr.openOutputStream(url);
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
        thumbOut.close();
        return thumb;
    } catch (FileNotFoundException ex) {
        return null;
    } catch (IOException ex) {
        return null;
    }
 }
}

最佳答案

尝试使用以下代码:

MediaScannerConnection.scanFile(
    context,
    new String[] { "path/to/file.jpg" }, 
    null,   // optional mine type
    null    // optional callback
);

当您将图像保存到存储中时,仍然需要告诉图库应用程序扫描您的文件。

关于java - 保存的(位图)图像需要一段时间才能出现在图库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50259964/

相关文章:

Java printf 与浮点矩阵的整数索引有关的问题

android - 如何最好地从 Android ListView 中的 URL 加载图像

android - 适用于 Android 的 Firebase 触发器 Razorpay 集成

android - 显示图像的 fragment 中的内存问题

Android:如何在 WebView 中显示位图?

Java 泛型 - Class 或 Class<?扩展某些类>

java - 集成测试服务器的数据库填充

java - 访问 jar 文件中的外部库 (jmf)

android - 使用 ImageView 时我的微调器不可见

android - 我如何在android中获取图像矩阵坐标?