我正在嘗試爲目前市場上的應用發佈關鍵更新。我需要查詢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