我的應用程序允許用戶使用外置攝像頭活動的一些數據收集的一部分,拍照,並在模型中供以後使用存儲所產生的文件路徑的Uri.toString() 。安卓:與URI或文件路徑解碼位圖文件時,未找到
當查看收藏的回收視圖後,該應用程序會慢下來,由於加載,以便圖像的大小,我實現谷歌的解決方案here,不過我在堆棧跟蹤越來越FileNotFoundExceptions
,並且圖像AREN」加載。視圖的其餘部分加載正常,應用程序不會崩潰。
如上所述,uriString
是格式爲content:foo/bar
的字符串。我嘗試過的解決方案包括。
- 在模型中存儲
file.getabsolutePath()
而不是uri.toString
。這改變了uriString中格式file:/foo/bar
,仍然無法正常工作 - 調用
Uri.parse(uriString).getPath()
- 從
uriString
創建一個新的文件對象並調用getAbsolutePath()
,並從中getPath()
。
要明確,致電imageView.setImageURI(Uri.parse(this.item.getPhotoURI()));
絕對有效。所以文件存在,uriString可以被Android解釋。有問題的功能如下。隨着我的堆棧跟蹤下面。
private Bitmap decodeSampledBitmapFromFile(String uriString, int reqWidth, int reqHeight)
{
// First we do this just to check dimensions (apparently)
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uriString, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// And then return with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(uriString, options);
}
我的堆棧跟蹤:
11-24 16:10:57.762 17199-17199/uk.mrshll.matt.accountabilityscrapbook E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Android/data/uk.mrshll.matt.accountabilityscrapbook/files/Pictures/JPEG_20161124_160913_-1155698030.jpg: open failed: ENOENT (No such file or directory)
文件名可能是問題。 JPEG_20161124_160913_-1155698030.jpg注意11556 ... 0開始之前的破折號,儘量避免在文件名的任何地方使用破折號。 – MikeOscarEcho