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
Android Browser Bug? Div Overflow Scrolling
Android: Starting an Activity for a Different Third Party App
Fcm (Firebase Cloud Messaging) How to Send to All Phones
How to Add a Bullet Symbol in Textview
How to Make a Phone Number Clickable on an iPhone or Android Phone to Make a Call in HTML
Textinputlayout Hint Doesn't Float Up After Updating Google Support Library
Android: How to Implement First Time Tutorial Like Go Launcher in My App
How to Create Custom Spinner Like Border Around The Spinner with Down Triangle on The Right Side
Android - Activity Not Found Exception
Using Multiple Text Colors in Android's Textview [ HTML.Fromhtml() ]
Save Webview Content to Android Storage and Load It
Insufficent_Storage_Memory Message While Install 50 Mb APK in Emulator
How to Discover Zeroconf (Bonjour) Services on Android? I'M Having Trouble with Jmdns
Android - Expandable Textview with Animation
Android Webview HTML5 Video Autoplay Not Working on Android 4.0.3
Gstreamer Examples in Android Studio
How to Set The Font Style to Bold, Italic and Underlined in an Android Textview