Skip to content Skip to sidebar Skip to footer

Null Pointer Exception When Trying To Compress Bitmap

I'm getting a null pointer exception when trying to compress a bitmap so I can send it to a ByteArrayOutputStream to get a byte array. I need this byte array so I can upload the im

Solution 1:

You're getting a null pointer exception because bitmap is null. Replace the line that goes

bitmap = BitmapFactory.decodeResource(getResources(), R.id.imgPreview);

with this

bitmap = ((BitmapDrawable) imgPreview.getDrawable()).getBitmap();

Solution 2:

if you are using PNG format then it will not compress your image because PNG is a lossless format. use JPEG for compressing yourimage and use 0 instead of 100 in quality.

then use

Bitmapdecoded= BitmapFactory.decodeStream(newByteArrayInputStream(stream.toByteArray()));

Solution 3:

Try using Bitmap.createScaledBitmap

Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);

Post a Comment for "Null Pointer Exception When Trying To Compress Bitmap"