Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thumbnail isn't refresh immediately

I'm making a file manager in which picture items have a small thumbnail.

I get thumbnail image by using MediaStore. Everything works fine. But when I rename or move a file, the thumbnail does not show up.

I've found a piece of code to refresh MediaStore:

getActivity().sendBroadcast(
  new Intent(Intent.ACTION_MEDIA_MOUNTED, 
             Uri.parse("file://" + Environment.getExternalStorageDirectory())));

It worked but I must wait 4 or 5 second and refresh, then the thumbnail updates.

How to get thumbnail of image immediately after rename or moving?

like image 463
Tai Dao Avatar asked Aug 06 '13 08:08

Tai Dao


People also ask

Why is my YouTube thumbnail not changing?

YouTube may disable custom thumbnails for certain search results when they're considered inappropriate for viewers. All custom thumbnail images must follow our Community Guidelines.

How long does it take for thumbnails to update?

A Youtube thumbnail normally changes in less than 10 secs if you have a good internet connection.

How long does it take for YouTube to update thumbnail?

How long does YouTube thumbnail take to update? After uploading thumbnails for YouTube video and save change, your thumbnail change comes in effect within 8-10 seconds.

Why is my custom thumbnail not working?

If you cannot upload a picture, then try first uninstalling the YouTube app, deleting the cache and then downloading it again, or uninstalling updates for it, and then clearing the cache and updating.


2 Answers

What happen if you use ACTION_MEDIA_SCANNER_SCAN_FILE instead of ACTION_MEDIA_MOUNTED, (i.e. trigger a refresh for a single file instead of for the complete directory hierarchy) ?

You will need to replace the URI of the directory with the URI of the file, obtained for example using Uri.fromFile().

When you move or rename a file you should refresh the old and the new URIs.

like image 101
bwt Avatar answered Oct 02 '22 20:10

bwt


The recommended way to update one specific image in Android is using ACTION_MEDIA_SCANNER_SCAN_FILE intent. And for smoother

You can check it at Basic Photo Handling Training in Android Developer Site.

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

If you want to show new thumbnail immediately for some missing files, you can do it by yourself. First, check the MediaStore as before, and if the returned thumbnail is null then generate your own one using ThumbnailUtils or BitmapFactory.

And, For handling a bitmap and displaying it, there is a quiet straightforward sample in Android Training Course.

like image 29
Chansuk Avatar answered Oct 02 '22 20:10

Chansuk