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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With