4

I have a problem concerning the BitMapFactory.decodeFile.

In my app, I want to user to be able to select an image from his/her device or take a photograph. This must then be displayed in an ImageView (medication_photo). I try to downscale the image as well because in my previous code I got OutOfMemory errors.

The problem is that after selecting an image, my app 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());
    }
}
Dhara
  • 6,587
  • 2
  • 31
  • 46
vaultboy
  • 173
  • 1
  • 1
  • 12
  • Are you sure the photo you're trying to load is actually in the location you're trying to open it from? – ThaMe90 Feb 20 '14 at 12:18
  • In the setPhoto function, this line of code did work: this.bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(path)); So I guess it's there. – vaultboy Feb 20 '14 at 12:24

3 Answers3

3

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();
}
Dev 9
  • 263
  • 2
  • 5
  • 1
    This will probably work, but it causes OutOfMemory errors. I tried to downscale the image before it allocates memory. – vaultboy Feb 20 '14 at 12:31
  • here is to use lazyloding. use below link..http://stackoverflow.com/questions/21598984/during-the-loading-several-images-in-imageview-get-out-of-memory-exception/21599501#21599501 – Dev 9 Feb 20 '14 at 12:35
1

Thanks for the responses, it put me in the right direction. Eventually this post helped me further: https://stackoverflow.com/a/2636538/3287175

Community
  • 1
  • 1
vaultboy
  • 173
  • 1
  • 1
  • 12
0

The path, you are using to create a File onbject in the following line...

File file = new File(path);

and in the below line, to decode a bitmap...

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

that isn't even a Folder or File directory. So, first of all, check your path properly that it contains a proper File path...this will solve your problem.

Suppose, you saved an image into SDCArd named as ABC.jpg then the proper image file path should be looked like as follows...

/mnt/sdcard/ABC.png
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • The 'File file = new File(path);' I forgot to remove, I do not use it. The path is passed to setPhoto from onActivityResult. – vaultboy Feb 20 '14 at 12:39
  • I still don't understand, it works when I use MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); So how can I find the right path? – vaultboy Feb 25 '14 at 10:56