2015-10-22 33 views
1

我正在使用存儲在外部存儲器中的一些大圖片。我解碼它們,調整它們的大小,並將小的位圖設置爲各種圖像瀏覽。從Uri到Path並返回Uri

我通過存儲他們的Uri來跟蹤這些大圖像。當應用程序停止時,我將Uri轉換爲路徑並將它們保存爲字符串是SQLite基礎:。

File myFile = new File(provider.getImageUri().toString()); 
cv.put(DBHelper.DB_IMAGEPATH, myFile.getAbsolutePath()); 

此時的圖像的路徑是這樣的:

/內容/媒體/ ..,一切工作正常。

當應用程序恢復我從數據庫中讀取路徑並將其轉換爲烏里:

File tempFile = new File(cursor.getString(imagePathColIndex); 
mUri = (Uri.fromFile(mFile)); 

現在,在新的穆裏的路徑是這樣的:
文件:///內容/ ...還有是IONotFound異常。

回答

1

你應該嘗試

File tempFile = new File(cursor.getString(imagePathColIndex)); 
mUri = Uri.parse(tempFile.getAbsolutePath()); 

測試用下面的代碼

String path = "/content/media"; 
Uri uri = Uri.fromFile(new File(path)); 
Log.e(TAG, uri.toString()); //print file:///content/media 

uri = Uri.parse(path); 
Log.e(TAG, uri.toString()); //print /content/media 

編輯 讀取圖像,你可以嘗試

BitmapFactory.decodeStream(new FileInputStream(Uri.parse(path))); 
+0

謝謝。從字符串路徑的角度來看,你的代碼是正確的。但是當我嘗試從該Uri獲取圖像時,仍然存在:java.io.FileNotFoundException:無內容提供者:/ content:/ media/external/images/media/520892 – rommex

+0

它將您的路徑視爲內容提供者Uri。這很奇怪。內容後爲什麼會有':'? – ThomasThiebaud

+0

當我稍後嘗試在該行中解碼來自該Uri的較小尺寸的圖像時:BitmapFactory.decodeStream(mContext.getContentResolver()。openInputStream(imageUri),null,o); – rommex