Tai Dao
Tai Dao

Reputation: 3437

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?

Upvotes: 6

Views: 2419

Answers (4)

androidyue
androidyue

Reputation: 1112

Actually sending Intent.ACTION_MEDIA_MOUNTED broadcast intent is really ugly. Read this post http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android As to renaming files. You should remove the older file from the library and then add the new one into the library. I think this could help you.

Upvotes: 0

Chansuk
Chansuk

Reputation: 792

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.

Upvotes: 1

Philippe Banwarth
Philippe Banwarth

Reputation: 17725

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.

Upvotes: 2

Paul Burke
Paul Burke

Reputation: 25584

Have you tried doing the scan directly on the directory you are changing? So instead of

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

something like

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/path/to/your/folder")));

An alternative would be to manually use ThumbnailUtils.

Upvotes: 0

Related Questions