Android Loading picture Oom error Solution

Source: Internet
Author: User

The first few days to do the project, party a request is pad (SAMSUNG P600 10.1 inch 2560*1600) on the pad display high-resolution large pictures.

SQLite uses bold way to store pictures, this access process does not say ha, a lot of online.

However, when loading/reading/displaying the picture will be reported oom error, the internet to check a lot of solutions also around a lot of detours, finally found the reason, the following from several aspects to explain the problem of oom solution.

(Thank you for the "size" reminder, otherwise I may have to keep in the dark All my life)

1.Android app Memory

Do an app development time, still do not want to expand the Android system to give the memory limit, some old model old system 16M, most of them are 24M.

Some big games use Dalvik.system.VMRuntime to interfere with GC processes (This class I haven't learned ... I just heard it soon).

It is said that in the development of the NDK, C can dynamically apply for extra memory space, but I did not use the NDK, will not be used in the future (personal reasons).

2. picture File size

Party A has given a large number of files of different sizes of pictures, in the LOAD database--read out, shown in the process, there are many oom, divided into:

A. Use Bytearraystream to create a stream when loading a picture, size height * Width * 4,oom;

B. When reading the picture, Oom, ditto;

C. Display the picture when the Oom,decoderesource function error, OOM;

Initially thought is the file size problem, later found that some 2M pictures can be displayed, but some 1M pictures do error, so within a certain range, can prove that the picture oom problem and file size independent.

3. Image size (resolution)

Investigate these can display the picture and cannot display the picture the difference, found the long width disparity is very big, those can successfully display the picture to be 1005*1500 size, but the other picture is 3000*5000 above.

I use Paint (a suitable for small white image processing software, although not as PS but the function is very powerful, the latest version needs to install NET 4.5) compressed image size, compression to 1005*1500, display success.

4. Function calls

Read the blog of the Great God:

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

Pick the most essential part of it:

Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set a large image, because these functions are completed decode, and ultimately through the Java layer of CreateBitmap to complete, you need to To consume more memory.

Therefore, instead of using the Bitmapfactory.decodestream method first to create a bitmap, and then set it to ImageView Source,decodestream the biggest secret is its direct call jni>> Nativedecodeasset () to complete the decode, eliminating the need to use the Java layer CreateBitmap, thus saving the Java layer of space.


If the config parameter is added to the read, it can be used to reduce the loaded memory effectively, thereby effectively blocking the throw out of


In addition, Decodestream directly take the picture to read the bytecode, not according to the machine's various resolutions to automatically adapt, after using the Decodestream, you need to configure the corresponding image resources in hdpi and mdpi,ldpi. Otherwise the same size (number of pixels) is the same on different resolution machines, and the displayed size is wrong.

It seems that reading the underlying source code is still useful, setimagebitmap and Setimageresource and Decoderesource in the execution of the process or call CreateBitmap to create a new bitmap, Creating bitmap can exacerbate memory consumption, so it is deprecated and should use the Decodestream method.

The compression process of the picture can also be set to a suitable percentage to control the size.

In the use of multiple pictures, please note that the flush and close of the stream (I did not run correctly when I flush and close in time), this remains to be researched, but in order to ensure a good habit or pay attention to it.

5. About the Options method

There are many articles on the Internet with bitmapfactory.options to use as a parameter of the decodestream time, this I have no specific experiment, which friend has been tested can reply to the Exchange ha

Android Loading picture Oom error Solution

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.