Mysterious Stacktrace in Android Developer Console (Bitmap Size Exceeds 32Bits)

Mysterious stacktrace in Android developer console (bitmap size exceeds 32bits)

if it 's a matter of quantity, you have to recycle your bitmaps.

if it's a matter of size (you can't load too big images), you have to load a lighter copy of your bitmap.

Here is the code to create a smaller image

Options thumbOpts = new Options();
thumbOpts.inSampleSize = 4;
Bitmap bmp = BitmapFactory.decodeFile(imagePath, mThumbOpts);

inSampleSize set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.

Use a power of 2 for it to be more efficient.

This post may be useful

Is there a maximum bitmap size when using getDrawingCache?

You can drop the buildDrawingCache method and just use canvas. Since you are building the view programmatically you also need to call measure() and layout() before you can get a bitmap.

The key lines are:

    tvText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tvText.layout(0, 0, tvText.getMeasuredWidth(), tvText.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(tvText.getWidth(), tvText.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tvText.draw(canvas);

Here is the full example:

private Bitmap getBitmap(Context context){

final int NUMBER_OF_LINES = 153;
final int width = 600;
final int fontSize = 24;

// create string with NUMBER_OF_LINES
StringBuilder testString = new StringBuilder();
for (int i = 0; i < NUMBER_OF_LINES; i++) {
testString.append("\n");
}

// Create TextView
TextView tvText = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tvText.setTextSize(fontSize);
tvText.setWidth(width);
tvText.setLayoutParams(params);
tvText.setBackgroundColor(Color.WHITE); // even setting the background color affects crashing or not
tvText.setText(testString);
tvText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tvText.layout(0, 0, tvText.getMeasuredWidth(), tvText.getMeasuredHeight());

// Create the bitmap from the view
Bitmap bitmap = Bitmap.createBitmap(tvText.getWidth(), tvText.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tvText.draw(canvas);

return bitmap;
}


Related Topics



Leave a reply



Submit