Android Bitmap OutOfMemory

Source: Internet
Author: User

In Android applications, image resources are the most memory-consuming resources. In addition, in the Android system, when reading Bitmap, the size of the image stack assigned to the virtual machine is only 8 Mb. If the size exceeds, an OutOfMemory exception occurs.

E/AndroidRuntime (697): java. lang. OutOfMemoryError
E/AndroidRuntime (697): at android. graphics. BitmapFactory. nativeDecodeAsset (Native Method)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeStream (BitmapFactory. java: 500)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeResourceStream (BitmapFactory. java: 353)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeResource (BitmapFactory. java: 376)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeResource (BitmapFactory. java: 406)
E/AndroidRuntime (697): at com. example. imagetoshow2.ImageAdapter. createReflectedImages (ImageAdapter. java: 66)
E/AndroidRuntime (697): at com. example. imagetoshow2.ImageAdapter. getView (ImageAdapter. java: 54)
E/AndroidRuntime (697): at android. widget. AbsSpinner. onMeasure (AbsSpinner. java: 193)

Solution:

1. Reclaim memory in time

If (bitmap! = Null &&! Bitmap. isRecycled () {// recycle and set it to null bitmap. recycle (); bitmap = null;} System. gc ();
Use the above Code in a proper place to recycle unused Code temporarily. Of course, system. gc should not be called frequently; otherwise, the system efficiency will be reduced.


2. Use BitmapFactory. Options to compress the image

 BitmapFactory.Options opts = new BitmapFactory.Options();   opts.inSampleSize = n;           bitmap = BitmapFactory.decodeStream(fis, null, opts);

Use inSampleSize to set the scale-down ratio. The default value is 0. Set a number greater than 0 to compress the image.

BitmapFactory. options opts = new BitmapFactory. options (); // set inJustDecodeBounds to true opts. inJustDecodeBounds = true; // use the decodeFile method to obtain the image width and height BitmapFactory. decodeFile (path, opts );

After inJustDecodeBounds is set to true, decodeFile () and other methods are used, and no space is actually allocated. That is, the decoded Bitmap is null and only options is calculated. outWidth and options. outHeight value. Before instantiating a Bitmap object by using the decodeFile () method of BitmapFactory. set inJustDecodeBound to false to get the image.


3. code optimization

To avoid an OutOfMemory exception that occurs when an application allocates Bitmap memory, an OutOfMemory exception is often caught in the Code for instantiating Bitmap.

<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> Bitmap bitmap = null; try {// instantiate Bitmap bitmap = BitmapFactory. decodeFile (path);} catch (OutOfMemoryError e) {//} </span>
Then perform some memory reclaim operations in the Catch part, or use cached images...

Always a good programming style and high-quality code structure are the ultimate pursuit of programmers ....

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.