2015-07-20 64 views
2

我試圖顯示通過攝像機意圖捕獲的圖片(觸發內置相機應用程序),它存儲在external SD-cardBitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions)返回FileNotFoundException

的文件路徑和文件名被存儲在字符串變量'mCurrentPhotoPath'

我覈實,拍攝的圖像存儲在使用ES File Explorer應用由'mCurrentPhotoPath'指定的位置,但BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)總是返回FileNotFoundException。這裏是我的代碼來存儲拍攝的圖像:

private void addPicToGallery() { 

    File f = new File(mCurrentPhotoPath); 
    //for debug purpose only 
    Log.i(MYTAG, "value of mCurrentPhotoPath in addPicToGallery: " +mCurrentPhotoPath); 
    Log.i(MYTAG, "value of f in addPicToGallery: " +f); 

    Uri contentUri = Uri.fromFile(f); 

    //for debug only 
    Log.i(MYTAG, "value of contentUri in addPicToGallery: " +contentUri); 

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    mediaScanIntent.setData(contentUri); 
    sendBroadcast(mediaScanIntent); 

} 

代碼爲ImageView顯示縮放圖像解碼:

private void setFullImageFromFilePath() { 

    Log.i(MYTAG, "Entered setFullImageFromFilePath method"); 

    // Get the dimensions of the View 
    int targetW = mImageView.getWidth(); 
    int targetH = mImageView.getHeight(); 

    Log.i(MYTAG,"var targetW in setFullImageFromFilePath is:" +targetW); 
    Log.i(MYTAG,"the targetH in setFullImageFromFilePath is:" + targetH); 


    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 

    Log.i(MYTAG, "the mCurrentPhotoPath in setFullImageFromFilePath is:" + mCurrentPhotoPath); 


    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW= bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    //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); 
    mImageView.setImageBitmap(bitmap) ; 

    } 

然而,BitmapFactory.decodeFile無法找到存儲在該位置的文件mCurrentPhotoPath導致FileNotFoundException

以下是logcat的輸出:

>07-20 21:28:11.897 32301-32301/org.assignment.lab.Pic I/Lab-Intents﹕ the mCurrentPhotoPath in setFullImageFromFilePath is:file:/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg 
07-20 21:28:11.907 32301-32301/org.assignment.lab.Pic E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg: open failed: ENOENT (No such file or directory) 
07-20 21:28:11.937 32301-32301/org.assignment.lab.Pic D/AndroidRuntime﹕ Shutting down VM 
07-20 21:28:11.937 32301-32301/org.assignment.lab.Pic W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418d0ce0) 
07-20 21:28:12.047 32301-32301/org.assignment.lab.Pic E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: org.assignment.lab.Pic, PID: 32301 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {org.assignment.lab.mydailyselfie/org.coursera.assignment.lab.Pic.MainActivity}: java.lang.ArithmeticException: divide by zero 

我在Android Kitkat 4.4.4電話調試該應用程序。

請建議在BitmapFactory.decodeFile方法中需要進行哪些修改,以便它可以存儲文件並將其顯示在ImageView中。

在此先感謝。

回答

8

mCurrentPhotoPath有一個前綴 「file:」。

由於其中的系統試圖指/file:/storage/sdcard0/etc...指向沒有安裝的磁盤。

刪除,並與

mCurrentPhotoPath = "/storage/sdcard0/Pictures/JPEG_20150720_212749_-232171051.jpg"; 

再試或者,如果您mCurrentPhotoPath永遠有前綴,用它

mCurrentPhotoPath = mCurrentPhotoPath.replace("file:", ""); 

希望這有助於之前做到這一點! :)

+0

非常感謝。刪除「file:」前綴後,問題就解決了。順便說一句,這段代碼是直接從developer.android.com。現在我想知道這個前綴在老版android版本中是否有必要。再次感謝。 – Niraj73

+0

棒極了!它用替換「文件」 –

相關問題