Reputation: 3409
I have large zoomable bitmaps with text, that I want to resize a little bit. Easiest way is to use inSampleSize, but the minimum value for it to work is 2, which is not acceptable (resizing image in half basically makes text unreadable).
createScaledBitmap works OK, but because of Android slow garbage collection for bitmaps, I sometimes run in to OutOfMemory in methods that follows it (I'm calling createBitmap after decode).
So, I wonder is there a way to reduce size of bitmap not in half and without memory issues?
Upvotes: 1
Views: 1428
Reputation: 57702
Welcome in my personal hell...
I am working with 2 bitmaps, one is a scaled version of the original which is scaled to fit on the screen. When I want to zoom into to make the text better readable, I switch to the original bitmap which will be used for zooming.
All my experience with huge bitmaps leads to one solution: Handle the complete life cycle of a bitmap by yourself. Don't just set a bitmap reference to null, recycle it before you release the reference. Also catch possible OOME and free memory there. Clean up everything and try the last bitmap decoding again.
I have multiple calls like this:
try {
try {
mBitmap = BitmapFactory.decodeFile(uri, mOptions);
}
catch (OutOfMemoryError e) {
freeMemory();
// retry
mBitmap = BitmapFactory.decodeFile(uri, mOptions);
}
}
catch (OutOfMemoryError e) {
Log.e("THREAD", "FATAL OOME ..." + mPageNumber, e);
}
Upvotes: 3