2015-07-28 136 views
0

我正在關注此tutorial拍照,保存,縮放和在Android中使用它。但是,當我嘗試打開/檢索保存的圖像時,出現Android: open failed: ENOENT (No such file or directory)錯誤。經過一番研究之後,我發現這篇文章假設這個問題伴隨着包含名字中的數字的文件,比如我的名字帶有當前的時間戳。我檢查了圖像保存在文件目錄中,並進行了日誌記錄以確保用於檢索它們的文件名與原始名稱匹配。 下面是給出了錯誤我的代碼的一部分:Android:打開失敗:ENOENT(沒有這樣的文件或目錄)錯誤

private void setPic(ImageView myImageView) { 
    // Get the dimensions of the View 
    int targetW = myImageView.getWidth(); 
    int targetH = myImageView.getHeight(); 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 
    Log.v("IMG Size", "IMG Size= "+String.valueOf(photoW)+" X "+String.valueOf(photoH)); 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    myImageView.setImageBitmap(bitmap); 
} 

這裏是什麼記錄顯示我:

E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/Pictures/JPEG_20150728_105000_1351557687.jpg: open failed: ENOENT (No such file or directory) 

,我試圖打開一個名爲圖片:JPEG_20150728_105000_1351557687.jpg

+0

這可能意味着,在您使用不存在的完整路徑的目錄:創建(使用.mkdirs()和_DO不forget_檢查返回值) – fge

+0

@DerGol ... LUM是一個完美有效的路徑(當然,Windows文件系統除外)。當然,你可能想知道它實際上是否使用'file:/ ...'作爲文件的URL,或者該方案是否完全在那裏 – fge

+0

@fge好的,我會更好地問它:你是否存儲你的圖片**在這裏**:'/ file:/ storage/sdcard0/...',在Android上? –

回答

1

我下載了你的代碼,並試圖在我的應用程序中使用它。發現前綴/file:導致FileNotFoundException

將您的方法替換爲以下方法。

private void setPic(ImageView myImageView) { 
    // Get the dimensions of the View 
    int targetW = myImageView.getWidth(); 
    int targetH = myImageView.getHeight(); 

    String path = mCurrentPhotoPath.replace("/file:",""); 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 
    Log.v("IMG Size", "IMG Size= " + String.valueOf(photoW) + " X " + String.valueOf(photoH)); 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions); 
    myImageView.setImageBitmap(bitmap); 
} 
相關問題