Android resolution JAVA.LANG.OUTOFMEMORYERROR:BITMAP size exceeds VM budget (RPM)

Source: Internet
Author: User

The problem that was encountered yesterday was to invoke Bitmapfactory.decodestream (This.getcontentresolver (). Openinputstream (URI)) from an input stream to get a bitmap report error. The first call is no problem, the second call will report the above memory overflow problem. And some mobile phone reported some mobile phone does not report. Study for a half-day finally solved. First, the following reason is analyzed, it should be that the image occupies more memory than the system virtual machine can allocate the maximum limit. Different phones may be assigned the same maximum value. Later the solution was to set the bitmapfactory.options.

    1. Bitmapfactory.options bitmapoptions = new Bitmapfactory.options ();
    2. Bitmapoptions.insamplesize = 4;
    3. Bitmap = Bitmapfactory.decodestream (This.getcontentresolver (): Openinputstream (URI), NULL, bitmapoptions);
Copy Code

Some say to add bitmapoptions.injustdecodebounds = True, but I add the resulting bitmap is null. You can do it after you remove it. Follow the above method to solve the problem. It is better to recycle bitmap when not in use:

    1. if (bitmap! = null &&!bitmap.isrecycled ())
    2. Bitmap.recycle ();
Copy Code

A picture of a great God occupies the memory algorithm:
Introduce a memory algorithm that occupies a process.
The base class for working with images in Android is bitmap, as the name implies, bitmaps. The memory-hogging algorithm is as follows:
The width*height*config of the picture.
If config is set to argb_8888, then the config above is 4. A 480*320 picture takes up memory that is 480*320*4 byte.
Someone said the concept of 8M, in fact, by default, the Android process memory consumption of 16M, because bitmap he in addition to holding data in Java, the underlying C + + Skia graphics library will also hold a Skbitmap object, Therefore the general picture occupies memory recommended size should not exceed 8M. This can be adjusted, you can set the parameters when compiling the source code.

Android resolution JAVA.LANG.OUTOFMEMORYERROR:BITMAP size exceeds VM budget (RPM)

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.