Android Bitmap Prevent memory overflow

Source: Internet
Author: User

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

1. Read the images using Bitmapfactory.decodestream () and bitmapfactorty.decodefile () to avoid using Bitmapfactory.decoderesource, the first two function calls Jni> >nativedecodeasset () to complete the decode, eliminating the need to use the Java layer CreateBitmap, thus saving the Java layer of space.

Note: By looking at source code Discovery Bitmapfactory.decoderesource eventually also calls Nativedecodeasset ()

2. Optimize heap memory allocation for Dalvik virtual machines

Can be called when the program is OnCreate

Private final static floattarget_heap_utilization = 0.75f; Vmruntime.getruntime (). Settargetheaputilization (target_heap_utilization);

Private final static int cwj_heap_size = 6* 1024* 1024x768;

Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); //Set minimum heap memory to 6MB size. Of course, the memory crunch can also be handled by manually interfering with the GC.

3.options Set Picture size options.insamplesize

Private ImageView Preview; Private String filename= "/sdcard/dcim/camera/2010-05-14 16.01.44.jpg"new= 2; // The image width is the original One-second, that is, the picture is the original One-fourth Bitmap B = bitmapfactory.decodefile (filename, options);p Review.setimagebitmap (b); filepath.settext (filename);

Android Bitmap Prevent memory overflow

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.