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.
The local file URL of the thumbnail image for the item.
To get the URL of a post thumbnail you need to add code to the theme template you are customizing. To learn more, refer to our guide on how to add custom code in WordPress. If you simply wanted to display the post thumbnail, then you could paste this code into the template you are working on, inside the WordPress loop.
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
public String[] getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor cursor = getActivity().getContentResolver().query(contentUri,
proj, null, null, null);
int path_index = cursor.getColumnIndexOrThrow(proj[0]);
int id_index = cursor.getColumnIndexOrThrow(proj[1]);
cursor.moveToFirst();
return new String[] { cursor.getString(path_index),
cursor.getLong(id_index) + "" };
}
From above getRealPathFromURI
now we have MediaStore.Images.Media._ID
, use this id to find thumbnail.
public static Bitmap getThumbnail(ContentResolver contentResolver, long id) {
Cursor cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media.DATA}, // Which columns
// to return
MediaStore.Images.Media._ID + "=?", // Which rows to return
new String[]{String.valueOf(id)}, // Selection arguments
null);// order
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String filePath = cursor.getString(0);
cursor.close();
int rotation = 0;
try {
ExifInterface exifInterface = new ExifInterface(filePath);
int exifRotation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
if (exifRotation != ExifInterface.ORIENTATION_UNDEFINED) {
switch (exifRotation) {
case ExifInterface.ORIENTATION_ROTATE_180:
rotation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotation = 90;
break;
}
}
} catch (IOException e) {
Log.e("getThumbnail", e.toString());
}
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
contentResolver, id,
MediaStore.Images.Thumbnails.MINI_KIND, null);
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(rotation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
return bitmap;
} else
return null;
}
To use above LOC
Updated
String[] imageInfo = getRealPathFromURI(Uri.parse("YOUR_IMAGE_PATH"));
yourImageView.setImageBitmap(getThumbnail(getActivity()
.getContentResolver(), Long.parseLong(imageInfo[1])));
Uri.parse("YOUR_IMAGE_PATH")
is contentUri
I am sure the ThumbnailUtils class can help you on this.
Bitmap thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
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,
public String getThumbnailPath(Uri uri) {
String[] projection = { MediaStore.Images.Media._ID };
String result = null;
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
cursor.moveToFirst();
long imageId = cursor.getLong(column_index);
cursor.close();
cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
getContentResolver(), imageId,
MediaStore.Images.Thumbnails.MINI_KIND,
null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
cursor.close();
}
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With