How to choose the optimal image size so that it does not exceed the VM budget?

In my application, users select images, and the program allows users to make changes to the images. Since there are many different devices for Android, my program crashes on some devices that are smaller than the heap. I want to calculate the optimal size for the user's phone so that it does not crash due to the VM budget. I added a screenshot from "Picsay Pro" that does exactly what I am looking for. I know about "BitmapFactory.Options", my only problem is to find a way to determine image sizes that will not allow the application to collapse due to the VM budget.

enter image description here

+7
source share
4 answers

Calculate the remaining free space by phone:

long freeMemory = (Runtime.getRuntime().maxMemory()) - (Debug.getNativeHeapAllocatedSize());

Where

Runtime.getRuntime (). maxMemory () - returns the total size of the heap limit (in bytes).

Debug.getNativeHeapAllocatedSize () - returns the amount of data already used by your application (in bytes).

Calculate the size of the bitmap that you use with this formula,

long Bytes_allocated = (height of the Image) * (width of the Image) * 4;

Compare freeMemory and Bytes_allocated to choose the right size for your application.

+12
source

I actually ended up compressing images over the phone for two reasons. One of them is download speed, and the other is a bunch of problems. You can try to do something like this, or at least post a stack trace!

+1
source

Android outofmemory raster error size exceeds vm budget in 2.3.3

I think this compression technique can help.

+1
source

So far, I have not found a reliable way to cope with image size and available memory. The problem is that memory is fragmented very quickly, so you may have 10 MB of free space, but there is no contiguous space for a 2 MB image. What is really needed is the size of the largest free space, but there seems to be no way to get this. A better way would be to defragment the memory, but there is no such function for this.

However, if your available memory is smaller than the image, you can be sure that you would run into it if you tried to use it, so it has some advantages to at least check before using it.

With tablets at the end of 2012 having a resolution of 1920x1280, we need 20 MB of continuous memory for one background image! It looks like some of these tablets now allow heaps of up to 256 MB, but throwing more VM space seems to solve it, we really need a memory defragmenter or a way to reserve space.

There is one trick that can help if the image size is not scaled or resized, and each resized image has the same size. And you are limiting your Android 3.0+ application:

 Options opt2 = new BitmapFactory.Options(); opt2.inBitmap = mBitmap; // reuse mBitmap to reduce out of memory errors mBitmap = BitmapFactory.decodeResource(getResources(), myDrawable, opt2); 

This will allow you to reuse the same mBitmap memory area for the image.

0
source

All Articles