I have a String URL pointing to an image stored on the external storage of my device:
String imageUrl = "/storage/emulated/0/DCIM/100MEDIA/IMAG0823.jpg"
I want to get query the MediaStore
to get the thumbnail for this image. This is what I do right now:
private String getImageThumbnailPath(Context ctx, String imageUrl){
Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
ctx.getContentResolver(), Uri.fromFile(new File(imageUrl)),
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
String url = "";
if( cursor != null && cursor.getCount() > 0 ) {
cursor.moveToFirst();
url = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
cursor.close();
}
return url;
}
However, calling this method and printing it's content shows nothing (the cursor is empty).
How do I query the MediaStore
for the thumbnail url associated with my image URL?
Edit
I've also tried to parse the Uri directly from the image URL, as so:
Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
ctx.getContentResolver(), Uri.parse(imageUrl),
MediaStore.Images.Thumbnails.MINI_KIND,
null);
But the result is the same.
Following code works if you have pick image from gallery, otherwise we can not have thumbnail, and we have to create thumbnail.
First you have to find the MediaStore.Images.Media._ID
From above
getRealPathFromURI
now we haveMediaStore.Images.Media._ID
, use this id to find thumbnail.To use above LOC
Updated
Uri.parse("YOUR_IMAGE_PATH")
is contentUriyou can use this method to resize and save thumbnail behind original image :
Also it will capture Memory Storage, But Faster than RunTime resize
You have pick an image from gallery, otherwise we can not have thumbnail. You can first launch a intent to pick an image from gallery like this.
after this in your onActivityResult do this
I am sure the ThumbnailUtils class can help you on this.
It returns a Bitmap and yous should check if it not null before using it. Sometimes, corrupted files return null thumbnails.
If you are not supporting anything below API level 8, you should be using this.
UPDATE
If you need the thumbnail path, please use this method,