Can I catch the memory exception in Android in a Bitmap calculation to decode an image file?

I tried to catch the decoding of the image file, but it could not catch an exception from memory, and the application would work.

I know some tricks in decoding an image file, for example, subsamples. But I need to enlarge the image to see the details, so I canโ€™t calculate it too much. For some newer devices, it will manage to allocate more memory to avoid being deleted from memory.

For some older devices, it cannot.

If I can configure the application for different devices, this will be welcome.

So I want: (1) I hope I can throw an exception out of memory, so if I catch it, I can reduce the image size. (2) Or, I hope I can get the size of the available memory to accommodate.

I search the Internet, I canโ€™t find the answers.

+6
source share
1 answer

First, you can see in the stack trace where OutOfMemoryError was thrown. If you didnโ€™t catch it, it is either because: a) you caught an exception instead of Throwable, or b) the error was thrown somewhere else, except where you had a catch statement. With OutOfMemoryError you cannot guarantee where it will be thrown. Usually this is where you deal with the bitmap, but it may be another thread that allocates memory at the same time.

Getting available memory on a device is trivial: http://developer.android.com/reference/android/app/ActivityManager.html#getMemoryClass ()

The Android team has a large set of articles about bitmaps and memory, if you haven't seen them yet: http://developer.android.com/training/displaying-bitmaps/index.html

Good luck

+5
source

Source: https://habr.com/ru/post/949509/


All Articles