Android image loading memory overflow Solution

Source: Internet
Author: User

Android image loading memory overflow Solution

This article mainly introduces how to solve the problem of memory overflow when loading images in Android. This article explains how to use BitmapFactory. Options to solve the problem of memory overflow. For more information, see

1. image processing is often encountered during Android software development. When converting an image to a Bitmap, because the image size is different, when a large image is encountered, the memory is exceeded. To solve this problem, the Android API provides BitmapFactory. options class.

2. Because Android has limits on the memory usage of images, memory overflow occurs when a few megabytes of large images are loaded. Bitmap loads all pixels (that is, the length x width) of the image into the memory. If the image resolution is too large, it will directly cause memory OOM. BitmapFactory is used only when BitmapFactory loads the image. options configures related parameters to reduce the loaded pixels.

3. BitmapFactory. Options parameters:

(1). Options. inPreferredConfig value to reduce memory consumption.

For example, the default value of ARGB_8888 is changed to RGB_565, saving half of the memory.

(2) set the Options. inSampleSize scaling ratio to compress the large image.

(3). Set Options. inPurgeable and ininputretriable to enable the system to return memory in time.

A: inPurgeable: if it is set to True, it indicates that the system can be returned when the system memory is insufficient. If it is set to False, it indicates that the system cannot be recycled.

B: ininputresumable: Specifies whether to copy data in depth. this parameter is used with inPurgeable. If inPurgeable is set to false, this parameter is meaningless.

(4). Use decodeStream instead of other methods.

DecodeResource, setImageResource, setImageBitmap, and other methods

4. Code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Public static Bitmap getBitmapFromFile (File file, int width, int height ){

 

BitmapFactory. Options opts = null;

If (null! = File & file. exists ()){

 

If (width> 0 & height> 0 ){

Opts = new BitmapFactory. Options ();

// Only the width and height of the image are returned, not a Bitmap object.

Opts. inJustDecodeBounds = true;

// The information is not saved in bitmap, but saved in options.

BitmapFactory. decodeFile (file. getPath (), opts );

// Calculate the image scaling ratio

Final int minSideLength = Math. min (width, height );

// The thumbnail size is a fraction of the original image size. Based on business needs.

Opts. inSampleSize = computeSampleSize (opts, minSideLength,

Width * height );

// Read the image again. Note that options. inJustDecodeBounds has been set back to false.

Opts. inJustDecodeBounds = false;

// Sets whether to copy data in depth, which can be used with inPurgeable.

Opts. ininputretriable = true;

// If it is set to True, the system can be returned if the system memory is insufficient. If it is set to False, the system cannot be recycled.

Opts. inPurgeable = true;

}

Try {

Return BitmapFactory. decodeFile (file. getPath (), opts );

} Catch (OutOfMemoryError e ){

E. printStackTrace ();

}

}

Return null;

}

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.