我正在尝试为目前市场上的应用发布关键更新。我需要查询MediaStore中的缩略图,并将缩略图加载到GridView中。到目前为止,我只需要根据我拥有的内容(这是缩略图的路径),在用户的外部存储上获取实际全图像的路径。当用户点击缩略图,分享,查看,移动和删除时,我需要能够执行4个操作,但我无法使用缩略图路径执行任何操作,我尝试了所有内容,而不是工作:/。以下是我在下面实施的一个片段,对此的任何帮助或指导将不胜感激!Android从Thumbnail获取图像路径?
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();
到目前为止,我已经尝试了所有使用imagePath成员,但是要么导致抛出异常或其他错误。有什么建议么?
你在gridview的imageadapter中有什么? – jxgn 2012-11-07 04:32:04
非常感谢你这个代码,但它没有给出所选图像的确切路径。当我选择第一个图像时,它将给出第二个图像的路径。 – Prabs 2015-09-04 06:57:53
[this helps](http://stackoverflow.com/a/6548026/4361127) – Prabs 2015-09-04 07:21:49