BitmapFactory.decodeFile returns fileNotFoundException

I have a problem with BitMapFactory.decodeFile.

In my application, I want the user to be able to select an image from his / her device or take a photo. Then it should be displayed in ImageView (medication_photo). I am trying to reduce the image because in my previous code I got OutOfMemory errors.

The problem is that after selecting the image, my application throws this exception:

FileNotFoundException: Unable to decode stream: 
java.io.FileNotFoundException: /content:/media/external/images/media/1499:
open failed: ENOENT (No such file or directory)

Code snippet:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAPTURE_PHOTO) {
        if (resultCode == RESULT_OK) {
            String result = data.getDataString();
            setPhoto(result);
        }
    }

    // browse photo
    if (requestCode == BROWSE_PHOTO) {
        if (resultCode == RESULT_OK) {
            String result = data.getDataString();
            setPhoto(result);
        }
        if (resultCode == RESULT_CANCELED) {

        }
    }
}



private void setPhoto(String path) {
    // re-scale image first
    try {
        File file = new File(path);
        Log.e("MedicationAdd.setPhoto->file",file.getAbsolutePath()); // this did not work 

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(path,options); // FileNotFound
        options.inSampleSize = scaleDownBitmap(options, options.outWidth, options.outHeight);
        Log.e("MedicationAdd.setPhoto->path",path);
        options.inJustDecodeBounds = false;
        this.bm = BitmapFactory.decodeFile(path,options); // FileNotFound

        this.medication_photo.setImageBitmap(this.bm);
    } catch (Exception e) {
        Log.e("MedicationAdd.setPhoto", e.toString());
    }
}
+3
source share
3 answers

Try this .... image uri

Uri imageUri = data.getData();
try {
   bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
   iv_profile.setImageBitmap(bm);
} catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
+3
source

path, File onbject ...

File file = new File(path);

, ...

this.bm = BitmapFactory.decodeFile(path,options);

Folder File. , , path, File... .

, SDCArd ABC.jpg, :

/mnt/sdcard/ABC.png
0

All Articles