0

I have an android app where I go to the camera activity and take a photo, and then I execute the following code:

        String s1 = file_uri.getPath();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeFile( s1, options);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50,  stream);

where the file_uri is the image path from the phone storage (it is not null) bitmap variable is null. Why?

  • You are using the uri.getPath() as the actual path to your file. Instead, you should [get the real path from Uri](http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore). – MohanadMohie Nov 05 '16 at 15:42
  • I will check it now. What is the difference between them? –  Nov 05 '16 at 15:51
  • I honestly don't know the difference. [This answer](http://stackoverflow.com/questions/176264/what-is-the-difference-between-a-uri-a-url-and-a-urn/1984225#1984225) might clarify things a little bit, but I still get confused about them. – MohanadMohie Nov 05 '16 at 16:47

2 Answers2

0

your image may be too big and you get out of memory

Andrew
  • 106
  • 1
  • 1
  • 13
0
    options.inJustDecodeBounds = true;
    bitmap = BitmapFactory.decodeFile( s1, options);

After those statements bitmap is always null'. That's normal.

    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile( s1, options);

Now you can have a bitmap. If not then the bitmap would be to big for available memory and decodeFile() had returned null;

Then you should resize your image. In variable options you can find the width and heigth of the image. With knowing pixels width and height you could calculate a scale factor. Or just say int scale = 8;`.

   options.inSampleSize = scale;    

After that do another call like

    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile( s1, options);

Presise code for this has been posted many times on stackoverflow.

greenapps
  • 10,799
  • 2
  • 13
  • 19