Thumbnail not updated immediately

I create a file manager in which image elements have a small sketch.

I get a thumbnail using MediaStore . Everything is working fine. But when I rename or move the file, the thumbnail is not displayed.

I found a code snippet for updating MediaStore :

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

This worked, but I have to wait 4 or 5 seconds and refresh, then refresh the thumbnail.

How to get a thumbnail of an image right after renaming or moving?

+6
source share
4 answers

What happens if you use ACTION_MEDIA_SCANNER_SCAN_FILE instead of ACTION_MEDIA_MOUNTED (i.e., run the update for a single file, and not for the full directory hierarchy)?

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

When moving or renaming a file, you must update the old and new URIs.

+2
source

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

You can check it out on “Basic Photography Training” on the Android Developer website.

 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 the new thumbnail immediately for some missing files, you can do it yourself. Check MediaStore first, as before, and if the returned sketch is null, generate your own using ThumbnailUtils or BitmapFactory .

And, to process the bitmap and display it, the Android Tutorial has a quiet simple pattern .

+1
source

Did you try to scan directly in the directory you are changing? Therefore, instead of

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

sort of

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

An alternative would be to manually use ThumbnailUtils .

0
source

In fact, sending Intent.ACTION_MEDIA_MOUNTED broadcast intent intentions is really ugly. Read this post http://androidyue.imtqy.com/blog/2014/01/19/scan-media-files-in-android Regarding renaming files. You must delete the old file from the library, and then add the new one to the library. I think this may help you.

0
source

Source: https://habr.com/ru/post/951097/


All Articles