0

I am trying to set the background for an ImageView using a saved path. However the following code crashes.

selectSavedImagefromGallery(imagePath);

Here imagePath = "/storage/sdcard0/Downloads/image.jpg" which is a valid path. It works when I set the image choosing it from the gallery. But I would like to save the path so it can be used at anytime.

private void selectSavedImagefromGallery(String savedImagePath) {

    Bitmap bm;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(savedImagePath, options);
    final int REQUIRED_SIZE = 200;
    int scale = 1;

    while (options.outWidth / scale / 2 >= REQUIRED_SIZE
            && options.outHeight / scale / 2 >= REQUIRED_SIZE)
        scale *= 2;
    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(savedImagePath, options);

    profile_pic.setImageBitmap(bm);
}

The error is being caused by this the last line:

profile_pic.setImageBitmap(bm);

What can I do to make it work?

Thanks.

fractal5
  • 2,034
  • 4
  • 29
  • 50
  • 1
    "However the following code crashes" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jan 10 '16 at 23:43
  • I think I understand why it's crashing. It's because there is no intent being sent to the method (unlike when I am picking an image from the gallery). I just don't know how to fix it. – fractal5 Jan 10 '16 at 23:49

0 Answers0