Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving large image to PNG

I've just encontered a tough problem...

Here's my story:

My device is Samsung Galaxy Note, normally, it takes pictures at a resolution of 3264 * 2448 pixels. I love fine pictures when am tring to find detailed information, but here I hate it because it's huge to handle in Android. I can read and display an image of this big resolution, I need to draw lines on top of it, then I need to save both picture and lines to a png. My headache is that my Android always gives me Out of memery exception, when am trying to create a mutable bitmap like this:

    Bitmap bmp = Bitmap.createBitmap(3264, 2448, Config.RGB_565);
    Canvas cv = new Canvas(bmp);
    //draw a line and save and restore canvas
    // I found an alternative solution yet proven to be uncorrect:
    myImageView.setDrawingCacheEnabled(true);
    myImageView.buildDrawingCache(true);
    Bitmap bmp = myImageView.getDrawingCache();
    //save bmp to png

When the

bmp.width * bmp.height * 4 > getScaledMaximumDrawingCacheSize()

exception jumps out. I've tested myImageView.measure(...) and myImageView.layout(...), didn't work for me. (Things are fine, when I test the same code using smaller images)

Any experts know how to kill this problem or throw a solution to me? Thanks!

like image 518
user1745958 Avatar asked Oct 26 '12 10:10

user1745958


1 Answers

Maybe not the definitve solution, but did you try to increase heap size?

For Android 2.2 or lower versions:

dalvik.system.VMRuntime.getRuntime().setMinimumHeapSize(sizeInBytes);

For Android 2.3 or or higher versions:

android:largeHeap="true"
like image 154
Federico Cristina Avatar answered Oct 14 '22 23:10

Federico Cristina