how to update thumbnails preview of android gallery after image edited

為{幸葍}努か 提交于 2019-12-08 07:16:57

问题


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


回答1:


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.



回答2:


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


来源:https://stackoverflow.com/questions/8700256/how-to-update-thumbnails-preview-of-android-gallery-after-image-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!