Java – Calling Bitmap.compress does not return – and there are no exceptions

Calling Bitmap.compress does not return – and there are no exceptions… here is a solution to the problem.

Calling Bitmap.compress does not return – and there are no exceptions

On Android, I sometimes do the following with images.

It never goes beyond the bitmapPicture.compress line – it just seems to sit there and hang.

The row above that gets the number of bytes returns 40,000.

I never see the compression complete or any other output after “compressing”.

try {

final int COMPRESSION_QUALITY = 100;
    String encodedImage;
    ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
    Log.e("Error","compress" + bitmapPicture.getByteCount());

bitmapPicture.compress(Bitmap.CompressFormat.PNG,
                    COMPRESSION_QUALITY, byteArrayBitmapStream);
    Log.e("Error","compress done");
    byte[] b = byteArrayBitmapStream.toByteArray();
    Log.e("Error","bytear");
    encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

Log.e("Error","JSONDATA encodedImage Returned");
    return encodedImage;

} catch (Exception e) {

ErrorLogger.AddError(e.getMessage(), 199);
    Log.e("Error","JSONDATA Error"+e.getMessage());
    return null;
}

Solution

Try to call

bitmapPicture.setConfig(Bitmap.Config.ARGB_8888);

Before compression. Otherwise, can you provide a little more? Can we see where you declare/load the bitmap?

Related Problems and Solutions