Get image from request thumbnails on Android
I have this code (found on this site somewhere):
public static List<MyImages> getImages(Activity context) {
List<MyImages> lst = new ArrayList<MyImages>();
Cursor cursor = getCameraThumbImages(context);
if (cursor != null) {
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int columnIndexPath = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
int columnIndexImagePath = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID);
int count = cursor.getCount();
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int imageID = cursor.getInt(columnIndex);
String path = cursor.getString(columnIndexPath);
Uri imgThmbPath = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID);
String hope = cursor.getString(columnIndexImagePath);
MyImages p2p = new MyImages(path, "" + imageID);
lst.add(p2p);
}
}
return lst;
}
This code allows me to access thumbnails of images on my phone. The problem is I can't see how to get the original image path from this.
Question: Given the thumbnail (or cursor), how do I get the original path of the image?
+3
source to share
1 answer
In the thumbnail, you have a MediaStore.Images.Thumbnails.IMAGE_ID field, you can get the image of the image associated with it. Then make a request to MediaStore.Images.Media and get the path to your photo from the MediaStore.Images.Media.DATA field.
EDIT
// First request thumbnails what you want
String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID};
Cursor thumbnails = contentResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
// Then walk thru result and obtain imageId from records
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {
String imageId = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.IMAGE_ID));
// Request image related to this thumbnail
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor images = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Media._ID + "=?", new String[] {imageId}, null);
if (cursor != null && cursor.moveToFirst()) {
// Your file-path will be here
String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
}
}
//Of course you need to restrict queries using selection and selection args params and get only rows that you really need
+2
source to share