1

I am trying to decode a bitmap but the function returns null and I don't know why.

The code:

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

if(requestCode == SELECT_FOTO) 
{
  Uri imgselect = data.getData();
  String imgpath = imgselect.getPath();
  File f = new File (imgpath);
  Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
  Toast.makeText(Insertarlugar.this, "Bitmap es" + bm, Toast.LENGTH_LONG).show();
}

The toast indicates me that bm is null. I changed f.getAbsolutePath() for f.getPath() but the result is the same. Uri imgselect and String imgpath have values. I don't use SD card and I obtain the bitmap from gallery.

How can I resize the bitmap?

Thanks.

2
  • 1
    check the imgpath value first Commented Aug 29, 2013 at 12:26
  • As I said in my post, I checked imgselect value. Is content/media/external/images/media/11295, so it has value. Commented Aug 29, 2013 at 18:59

2 Answers 2

3

try this one and check before imagepath is not null

Uri imgselect = data.getData();
String imgpath = imgselect.getPath();
if(imgpath !=null)
{
  Bitmap bm = BitmapFactory.decodeFile(imgpath);
}

if the image have in large the it can not decode the imagepath so you try this and give the width and height as 60 and 60

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
            int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        return bmp;
        }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
             }
         }
         return inSampleSize;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

I fixed the problem. I didn't display the image on the screen. Thanks for answering.
Perfect Solution...Worked for me.
@sunil kumar can you please solve me this problem almost same stackoverflow.com/questions/43868432/…
0

No need to use an actual file because you are referring to a Content Provider (which will do this for you)

Uri imgselect = data.getData();
Bitmap bm = BitmapFactory.decodeFile(imgselect.getPath());

Cannot check if it works, you may need to decode the Uri first.

1 Comment

I tried what you typed but it doesn't run. I obtain null decoding bitmap. I decoded the Uri but the result is the same. I am stuck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.