how to update thumbnails preview of android galler

2019-06-03 05:43发布

possible duplicate

Refreshing the thumbnail using MediaScanner

I have a problem regarding the update of thumbnails in the android in built-in gallery. Actually I have opened, edited and saved the image which was selected in the gallery.

I open leave the gallery and open the gallery again, then the thumbnail for that image is not updated. However, I pick the image, it is the edited version indeed.

Does anybody have solution about this? I have search but I'm getting the answer about refreshing the gallery using sendBroadcast(), which does not seem to update the image previews.

Edit

I don't want to delete the existing file because suppose user want to save file with different name then that file did not affected

2条回答
beautiful°
2楼-- · 2019-06-03 06:20

I found a workaround for this problem, before you request a new thumbnail you need to delete old one. Use MediaStore and ContentResolver for this

private static void removeThumbnails(ContentResolver contentResolver, long photoId) {
Cursor thumbnails = contentResolver.query(Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails.IMAGE_ID + "=?", new String[]{String.valueOf(photoId)}, null);
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {

    long thumbnailId = thumbnails.getLong(thumbnails.getColumnIndex(Thumbnails._ID));
    String path = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.DATA));
    File file = new File(path);
    if (file.delete()) {

        contentResolver.delete(Thumbnails.EXTERNAL_CONTENT_URI, Thumbnails._ID + "=?", new String[]{String.valueOf(thumbnailId)});

    }

}

You can get photoId from its URI, to get URI from file name just create File and parse URI from it

Uri uri = Uri.fromFile(file);
查看更多
Root(大扎)
3楼-- · 2019-06-03 06:43

Two suggestions.

  • Try getThumbnail. From the javadoc, I would expect it to regenerate an up-to-date thumbnail.
  • If that doesn't update the thumbnail, try deleting the thumbnail.
查看更多
登录 后发表回答