2015-07-20 1 views
2

J'essaie d'afficher l'image capturée par l'intention de la caméra (en déclenchant l'application intégrée de la caméra) qui est stockée dans external SD-card.BitmapFactory.decodeFile (mCurrentPhotoPath, bmOptions) renvoie FileNotFoundException

Le chemin du fichier et le nom du fichier sont stockés dans la variable de type chaîne 'mCurrentPhotoPath'.

J'ai vérifié que l'image capturée est stockée dans l'emplacement spécifié par 'mCurrentPhotoPath' en utilisant l'application ES File Explorer, mais le BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions) retourne toujours FileNotFoundException. Voici mon code pour stocker l'image capturée:

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); 

} 

code pour décoder une image à l'échelle pour l'affichage dans 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) ; 

    } 

Cependant, BitmapFactory.decodeFile n'est pas en mesure de trouver le fichier à l'emplacement stocké dans mCurrentPhotoPath ayant pour résultat FileNotFoundException.

ci-après le LogCat sortie:

>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 

Je débogage cette application dans le téléphone Android Kitkat 4.4.4.

S'il vous plaît suggérer quelle modification nécessaire dans BitmapFactory.decodeFile méthode afin qu'il puisse obtenir le fichier stocké et l'afficher en ImageView.

Merci d'avance.

Répondre

8

Votre mCurrentPhotoPath a un préfixe "file:".

En raison de laquelle le système tente de se référer à /file:/storage/sdcard0/etc... qui pointe vers aucun disque monté.

Retirez et tenter à nouveau avec

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

Ou, si votre mCurrentPhotoPath aura toujours le préfixe, le faire avant de l'utiliser

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

Hope this helps! :)

+0

Merci beaucoup. Après avoir supprimé le préfixe "file:", le problème a été résolu. D'ailleurs, ce code vient directement de developer.android.com. Maintenant, je me demande si ce préfixe était nécessaire dans la version Android plus ancienne. Merci encore. – Niraj73

+0

Superbe! cela a fonctionné avec le remplacement de "fichier" –