Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose optimum image size to not exceed VM budget?

In my app users choose images and program lets users to make changes on images. Since there are a lot of different android devices out there my program crashes on some of devices which less heap size. I want to calculate the best fit dimensions for user's phone so it won't crash because of VM budget. I have added a screenshot from "Picsay Pro" which is making exactly what i am looking for. I do know about the "BitmapFactory.Options" my only problem is to find a way to decide image dimensions which won't let crash the app because of VM budget.

enter image description here

like image 231
dirhem Avatar asked Apr 01 '11 15:04

dirhem


2 Answers

Calculate the Free Space Remaining on the phone:

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

where,

Runtime.getRuntime().maxMemory() - returns the Total Heap Limit size (in Bytes).

Debug.getNativeHeapAllocatedSize() - returns the Amount of Data already used by your application (in Bytes).

Calculate the size of the Bitmap you are using by this formula,

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

Compare freeMemory and Bytes_allocated to select the appropiate size for your application.

like image 200
AjOnFire Avatar answered Sep 28 '22 06:09

AjOnFire


I actually ended up compressing images on the phone for two reasons. One was upload speeds and another one was heap problems. You can try doing something similar, or at least post the stack trace!

like image 29
Kevin Parker Avatar answered Sep 28 '22 08:09

Kevin Parker