简体   繁体   English

保存的(位图)图像需要一段时间才能显示在图库中

[英]Saved (bitmaps) images take a while to appear in gallery

I have an app that do some edits on bitmaps and then save them to media store . 我有一个应用程序,可以对位图进行一些编辑,然后将其保存到媒体存储中。 It saves successfully to the file browser but it takes a time to appear in the gallery (up to hours ) or until i restart the device . 它已成功保存到文件浏览器,但是要花一些时间才能显示在图库中(最多几个小时),或者直到我重新启动设备为止。

Note : the image is saved instantly to the file browser and it's just fine 注意:图像会立即保存到文件浏览器中,就可以了

Here is my imagesaver class : 这是我的imagesaver类:

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

You aren't telling the system the file exists. 您不是在告诉系统文件存在。 You need to do that, it doesn't magically detect it. 您需要这样做,它不会神奇地检测到它。 Check out MediaScannerConnection for how to do that. 请查看MediaScannerConnection了解如何执行此操作。

Try using the below code: 尝试使用以下代码:

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

At the point you saved the image in storage, is still needed to tell the Gallery Application to scan your file. 在您将图像保存到存储中的那一刻,仍然需要告诉Gallery Application扫描您的文件。

You have a few options. 您有几种选择。 You can tell Android to rescan itself, "not recommended" as that is time consuming and slow. 您可以告诉Android重新扫描自身,“不推荐”,因为这既耗时又缓慢。 Or you can update Android with just the one new record. 或者,您也可以只更新一条新记录来更新Android。

So what I typically do when I create or delete an image. 因此,当我创建或删除图像时通常要做什么。 I make an AsyncTask for updating the media on the android device with a post execute for success. 我制作了一个AsyncTask,用于通过成功执行发布来更新android设备上的媒体。

Simply put this code in your onExecute. 只需将这段代码放在您的onExecute中即可。

 MediaScannerConnection.scanFile(mContext, new String[]{ mPassedInURLToAddedOrRemovedImageString }, null, null);

Android will do the rest. 其余的将由Android完成。 But I do recommend putting in an async task. 但是我建议您放置一个异步任务。

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

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