2013-02-27 103 views
2

我想從SD卡擴展位圖並將其寫入手機內存。然後在稍後解碼它將其添加到HashMapOpenInputStream返回文件未找到異常,但文件存在

的問題是,我收到文件未發現異常儘管路徑是正確的,縮放後的圖像存在(我檢查)

這裏是節約部分

 Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options);   
     File imageRootPath = getFilesDir(); 
     File imageRoot = new File(imageRootPath, imagUri.getLastPathSegment()+".png"); 
     FileOutputStream out = new FileOutputStream(imageRoot); 
     yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 90, out); 

這裏是部分時,我讀文件

try { 
Uri mainImgeUri = Uri.parse(imageRoot.toString()); 
File imageFile = new File(mainImgeUri.toString()); 
if(imageFile.exists()){ 
    System.out.println("it does"); 
} 
InputStream imageStream = ListPropertiesBaseActivity.this.getContentResolver().openInputStream(mainImgeUri); // I am getting file not found error 

Bitmap yourSelectedImage =BitmapFactory.decodeStream(imageStream); 
hmBitmap.put(ID, yourSelectedImage); 
imageStream.close(); 
} catch (Exception e) { 

e.printStackTrace(); 
} 

難道是OpenInputStream不能從手機內部存儲器中讀出?或者可能是儲蓄導致的形象不好?

雖然我能夠通過手動瀏覽到該文件,並打開來查看它

請注意的System.out.println執行所以這意味着該文件存在

回答

2

你試過記錄imageStream來查看uri實際是什麼嗎?

+0

那麼,因爲我在填充imageStream的行處發生異常,所以它甚至沒有填充 – Snake 2013-02-27 17:32:26

+0

我擔心解析從imageRoot.toString()不會給你一個真正的URI到文件的位置。 您可以嘗試Uri mainImgeUri = Uri.fromFile(imageRoot)以獲取實際的文件:// uri與openInputStream一起使用。 – speedynomads 2013-02-27 17:41:59

+0

我記錄了URI並且它有正確的路徑。我會用其他東西更新我的代碼 – Snake 2013-02-27 17:47:09

1

使用創建File對象你mainImgeUri uri和檢查文件是否存在,將該文件傳遞到openInputStream()方法

+0

我把代碼來檢查文件是否存在。它確實如此。 OpenInputStream只接受URI,所以我不能傳遞文件,但文件存在 – Snake 2013-02-27 17:04:22

0

我找到了。那麼我找到了一個解決方法,但我從來沒有能夠解釋以前的behviour和爲什麼它不會工作。我換成這個getContentResolver與

FileInputStream fis = new FileInputStream(imageFile); 
Bitmap yourSelectedImage =BitmapFactory.decodeStream(fis); 

這個工作。任何想法爲什麼!雖然