We are working on a Android app that captures images and converts the same to Bitmap to be posted to a Rest service.
While creating the Bitmap we have BitmapFactory.Options.isSample Size configured to value of 4 and this works fine on all device except for HTC One. The image in HTC one is distorted.
When we change the value to 1 it shows a good image on HTC one but the application crashes on all other devices because of Memory issues.
The issue is related to Heap size available.
- What is the reason that HTC one shows a distorted image on HTC one.
- Why is application crashing on S3 which also has the same build version of 16 but crashes due to heap size unavailability.
- Why does all lower version phone like droid works fine for sample size of 4.
- Whats the best way to check available memory space for an app and use the available for working with BITMAP.
When we configure android:largeHeap in manifest file this seems to work but it does not seem to be a dependable solution.
I was able to find the size of the picture taken using camera parameters and then arrive at optimal sample size of bitmap. The below code solves issue. But is it right way of doing or should i need to compute sample size too instead of going with 1 and 4.
private Bitmap createImaegBitmap(byte[] data)
{
BitmapFactory.Options opt;
opt = new BitmapFactory.Options();
opt.inTempStorage = new byte[16 * 1024];
Parameters parameters = ((ReceiptCollection)getApplication()).getParams();
Size size = parameters.getPictureSize();
int height11 = size.height;
int width11 = size.width;
float mb = (width11 * height11) / 1024000;
if (mb > 4f)
opt.inSampleSize = 4;
else if (mb > 3f)
opt.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,opt);
return bitmap;
}