Reputation:
I have an app which uses cursors to load thumbnails (into a gridview) from the mediastore:
CursorLoader cursorLoader = null;
if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= android.os.Build.VERSION_CODES.HONEYCOMB) {
String[] from = { MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.TITLE };
projection = from;
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
Uri queryUri = MediaStore.Files.getContentUri("external");
cursorLoader = new CursorLoader(this, queryUri, projection,
selection, null, MediaStore.Files.FileColumns.DATE_ADDED
+ " DESC");
} else {
String[] from = { MediaStore.MediaColumns.DATE_ADDED };
projection = from;
Uri sourceImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
cursorLoader = new CursorLoader(this, sourceImageUri, null, null,
null, MediaStore.MediaColumns.DATE_ADDED + " DESC");
}
Cursor cursor = null;
if (cursorLoader != null) {
cursor = cursorLoader.loadInBackground();
mGalleryAdapter = new GalleryAdapter(this,
android.R.layout.simple_list_item_1, cursor, projection,
to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
gallery.setAdapter(mGalleryAdapter);
gallery.setOnItemClickListener(myOnItemClickListener);
}
However, some newer images are loaded without thumbnails, which is rather silly. The thumbnails are only generated when I open the gallery app (Photos) on my phone.
How can I refresh all the thumbnails programatically on the device so there are no images left blank without a thumbnail?
I would like this to work on all Android version, including 4.4. Thanks in advance :)
Upvotes: 2
Views: 1924
Reputation: 199
Use MediaStore.Images.Thumbnails.getThumbnail()
or MediaStore.Video.Thumbnails.getThumbnail()
- it's a blocking class and generates a new bitmap if it doesn't find one. Much simpler.
You simply need the MediaStore ID of the file, which I assume you have, and you can use the standard getContentResolver() in your Activity or through a context. This method also allows for you to get a Bitmap directly in MINI or MICRO sizes.
As indivisible says, you can use
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
However, that will not work on Android 4.4 (I think), so consider using the MediaScanner class for API 19+.
Upvotes: 1
Reputation: 5012
You should be able to trigger a rescan with this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
You may need to alter the path to suit your needs.
And if you wanted to wait for the scan to complete before moving forward you will need to register a receiver for the ACTION_MEDIA_SCANNER_FINISHED
intent.
Details and discussion on that here/RdvA7y4MqfE
kaiwid23 suggests this doesn't work for 4.4+. In that case you can manually control the scanner with a MediaScannerConnection
.
You will want to use the MediaConnection.scanFile()
method. I don't know if it works recursively if supplied a directory or whether it just accepts single paths though.
In this case you pass an OnScanCompletedListener
in the constructor so monitoring for the scan to complete is actually much easier.
Upvotes: 2