问题
I'm trying to ship a pivotal update to my app currently on the market. I need to query the thumbnails in the MediaStore, and load the the thumbnails into a GridView. So far so good, now I just need to get the path of the actual fullsized image on the user's External Storage based only on what I have(which is the path to the thumbnail). I need to be able to perform 4 actions whenever a user clicks a thumbnail, Share, View, Move and Delete...but I can't do any of these with just the thumbnail path, I've tried everything, doesn't work :/. Here's a snippet of my implementation below, any help or guidance on this would be greatly appreciated!
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int columnIndex = 0;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
if(cursor != null){
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
imagePath = cursor.getString(columnIndex);
FileInputStream is = null;
BufferedInputStream bis = null;
try{
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
useThisBitmap = Bitmap.createScaledBitmap(bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
}catch(Exception e){
//Try to recover
}
finally{
try{
if(bis != null){
bis.close();
}
if(is != null){
is.close();
}
cursor.close();
projection = null;
}catch(Exception e){
}
}
}
Toast.makeText(this, " " + imagePath, Toast.LENGTH_SHORT).show();
So far I've tried everything using the imagePath member, but that either causes exceptions to be thrown or other errors. Any suggestions?
回答1:
I've found the solution. Before I was querying the MediaStore Content Resolver and grabbing Thumbnails(not what I needed anyways). This optimization allows me to search the MediaStore in the same exact way, but this time I'm searching for the ACTUAL Images, and I'm returning the Thumbnail MICRO_KIND. The images are then cached in an array of Bitmaps and then used to populate the GridView. I am now able to get the path to the actual, full size image on the device..in case I need to create a URI instance or a File using that path, now I can easily. For anyone else having this or a similar issue Android getting path to image from Thumbnail?, here's the solution code below. Btw, I'm doing this in an AsyncTask and showing/dismissing a loading dialog after all images on device have been displayed.
/*----------------------------ASYNC TASK TO LOAD THE PHOTOS--------------------------------------------------------*/
public class LoadPhotos extends AsyncTask<Object, Object, Object>{
@Override
protected Object doInBackground(Object... params) {
final String[] columns = { MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
AllPhotosActivity.count = imagecursor.getCount();
AllPhotosActivity.windows = new Bitmap[AllPhotosActivity.count];
for (int i = 0; i < AllPhotosActivity.count; i++) {
imagecursor.moveToPosition(i);
//i = index;
int id = imagecursor.getInt(image_column_index);
windows[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
imagecursor.close();
return null;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
}
@Override
protected void onProgressUpdate(Object... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
And here's my onItemClick() The "filename" member is what you would use to create instances of URIs or whatever you need to do with the image.
public void onItemClick(AdapterView<?> arg0, android.view.View v,
int position, long id) {
String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, null);
final int dataColumnIndex = actualimagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
final int idColumIndex = actualimagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
actualimagecursor.moveToPosition(position);
filename = actualimagecursor.getString(dataColumnIndex);
final long imageId = actualimagecursor.getLong(idColumIndex);
回答2:
Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
use this code, i think it will work. Basically its the same id for both thumbnail and the original image, so the position for both thumbnails and images will be the same.
来源:https://stackoverflow.com/questions/9279553/android-getting-path-to-image-from-thumbnail