2012-06-04 38 views
1

我已經在圖像視圖中設置了圖像。現在我想保存這個狀態,並且需要知道該圖像的圖像路徑。有沒有辦法做到這一點?是否可以在圖像視圖中獲取圖像的圖像路徑?

UPDATE:

// Decode, scale and set the image. 
      Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath); 
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true); 
      myBitmap.recycle(); 
      myBitmap = null; 

      mImageView.setImageBitmap(scaledBitmap); 
+0

爲什麼你需要一個圖像路徑,當你已經有了selectedImagePath?你的意思是爲了獲得縮放位圖的路徑嗎?恕我直言,縮放位圖存儲在內存中,你不能得到它的路徑(除非你堅持下去).. p.s.檢查這是否你想堅持位圖http://stackoverflow.com/questions/649154/android-bitmap-save-to-location – sinek

+0

,我怎樣才能得到縮放圖像的路徑? – user1420042

+0

你不能,你只能得到它的參考。另一方面,如果真的需要的話,你可以使用該引用並將映像刷新到磁盤以備後用(儘管我仍不明白爲什麼你需要使用路徑而不是圖像引用?)。 – sinek

回答

3

不是直接的,一旦圖像設置上ImageView它變成了Drawable和一切被遺忘。但是,您可以使用標籤來達到此目的。任何視圖都可以有一個與之關聯的標籤,代表一些關於該視圖的元數據。你可以這樣做:

ImageView iv; //Declared elsewhere 
Uri imagePath = Uri.parse("..."); 
//Set the image itself 
iv.setImageURI(imagePath); 
//Add the path value as a tag 
iv.setTag(imagePath); 


//Sometime in the future, retrieve it 
Uri path = (Uri)iv.getTag(); 

你也可以考慮創造ImageView一個子類來封裝這些額外的功能,使您的代碼變得更容易閱讀和維護。

HTH

+0

這對我的代碼看起來如何?什麼時候設置標籤?查看更新。 – user1420042

+0

在最後一行(即'setImageBitmap()'後面)添加'mImageView.setTag(selectedImagePath)'行。然後,根據路徑是什麼對象類型(String,Uri等),您可能需要修改檢索示例中的強制轉換。 – Devunwired