Android image loading OOM error Solution

Source: Internet
Author: User

Android image loading OOM error Solution

When working on the project a few days ago, Party A requested that the high-resolution large image be displayed on the PAD (SAMSUNG P600 10.1-inch 2560*1600.

SQLITE uses the BOLD method to store images. This access process will not be mentioned. There are a lot of images stored on the Internet.

However, the OOM error will be reported when loading, reading, and displaying images. I checked a lot of solutions online and found the cause, the following describes the solution to the OOM problem from several aspects.

(Thank you for your "size" reminder. Otherwise, I may be stuck in the dark all my life)

1. Android APP memory

When developing an APP, do not expand the memory ceiling granted by the Android system. For some old models, the memory ceiling is 16 MB, most of which are 24 MB.

Some large Games use dalvik. system. VMRuntime to interfere with the GC process (I have not learned this class before... I just heard about it ).

It is said that when developing with NDK, C can dynamically apply for excess memory space, but I have never used NDK and will not use it in the future (for personal reasons ).

2. Image File Size

Party A provides a lot of pictures of different file sizes. In the process of loading data to the database> reading data> displaying data, there are a lot of OOM, which can be divided:

A. Use ByteArrayStream to create a stream when loading an image. The size is Height * Width * 4, OOM;

B. OOM when reading images, same as above;

C. When the image is displayed, the OOM and decodeResource functions report an error. OOM;

At first, I thought it was a file size problem. Later I found that some images of 2 m can be displayed, but some images of 1 m do report errors, so within a certain range, it can be proved that, the OOM problem is not related to the file size.

3. image size (resolution)

Looking at the differences between the images that can be displayed and those that cannot be displayed, it is found that the length and width are quite different. The size of the images that can be successfully displayed is 1005*1500, other images are 3000*5000 or more.

I used PAINT (an image processing software suitable for Tom, although not as powerful as PS, but the latest version needs to install NET 4.5) to compress the image size to 1005*1500, displayed successfully.

4. function call

Read the blog of this great God:

Http://blog.csdn.net/huangbiao86/article/details/8072128

Extract the most essential parts:

Do not use setImageBitmap, setImageResource, or BitmapFactory. decodeResource to set a large image. After decode is completed, these functions are implemented through the createBitmap on the java layer and consume more memory.

Therefore, use BitmapFactory first. the decodeStream method creates a bitmap and sets it as the source of ImageView. The biggest secret of decodeStream is that it directly calls JNI> nativeDecodeAsset () to complete decode, you no longer need to use the createBitmap on the java layer, thus saving the java Layer Space.


If the configuration parameter of the part is added during reading, the load Memory can be effectively reduced to prevent the throwing out of Memory exception.


In addition, decodeStream reads bytecode directly from images and does not automatically adapt to various machine resolutions. After decodeStream is used, it must be in hdpi and mdpi, configure the corresponding image resources in ldpi. Otherwise, the images are of the same size (number of pixels) on machines with different resolutions. The displayed size is incorrect.

It seems that reading the underlying source code is still very useful. setImageBitmap, setImageResource, and decodeResource call createBitmap during execution to create a new bitmap. Creating bitmap will increase memory consumption, therefore, decodeStream is not recommended.

You can set an appropriate percentage to control the image size during compression.

When using multiple images, pay attention to the flush and close of the stream (when I did not flush and close in time, it also runs correctly. This is to be studied, but to ensure a good habit, pay attention to it ).

5. About the options method

There are still many articles on the Internet that use BitmapFactory. Options as a parameter for decodeStream. I have not tried this experiment yet. If any of my friends have tried this experiment, I can reply to them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.