2016-11-24 47 views
0

我的應用程序允許用戶使用外置攝像頭活動的一些數據收集的一部分,拍照,並在模型中供以後使用存儲所產生的文件路徑的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) 
+0

文件名可能是問題。 JPEG_20161124_160913_-1155698030.jpg注意11556 ... 0開始之前的破折號,儘量避免在文件名的任何地方使用破折號。 – MikeOscarEcho

回答

0

如前所述,uriString中是在內容格式的字符串:富/酒吧。

不是根據你的錯誤。你的錯誤表明你正試圖通過"file:/storage/emulated/0/Android/data/uk.mrshll.matt.accountabilityscrapbook/files/Pictures/JPEG_20161124_160913_-1155698030.jpg"decodeFile()。該字符串表示的方案是file,而不是content。更重要的是,decodeFile()不採取Uri(或Uri的字符串表示),而是一個文件的路徑和文件路徑沒有計劃。

如果您期望擁有的contentfile方案的一些組合:

  • 守住Uri(或最壞情況下,重新解析Uri出來的字符串)
  • 使用ContentResolveropenInputStream()以獲得由Uri代表的內容的InputStream(因爲openInputStream()同時支持contentfile方案)
  • 使用decodeStream()代替decodeFile()
+0

對不起。這是一個不同的堆棧跟蹤,正如我在文章中提到的,我嘗試更換urlString的創建方式。嘗試您的建議重新ContentResolver – Mrshll1001