Android Learning notes: How to display pictures efficiently, avoid memory overflow and ImageView cannot display large size pictures

Source: Internet
Author: User

Because the memory resources of the phone are limited, the memory that each app can use is restricted. Now, high-resolution shots are often very large. If you do not pay attention to the method when loading, it is likely to cause the java.lang.OutofMemoryError: bitmap size exceeds VM budget . Exception that caused the app to run out of the way.

In addition, ImageView supported image size is also limited, such as the entire app, although only a picture, the image size does not exceed the entire app memory limit. However, this image is larger than the maximum value of ImageView, which is problematic. You need to take a method to reduce the size of the loaded picture when the picture is loaded. The specific strategy looks at the following introduction.

When a picture object is actually created and loaded (which needs to actually consume memory), we can get the size information of the image first. Here we can use the Bitmapfactory.options class. Bitmapfactory.options This class, there is a field called Injustdecodebounds. The description of this member in the SDK is this:
"If set to True, the decoder would return null (no bitmap), but the ... fields would still be set, allowing the caller to Query the bitmap without has to allocate the memory for its pixels. "

That is, if we set it to true, then Bitmapfactory.decodefile (String path, Options opt) will not really return a bitmap to you, but it will take it back to you with wide, high-profile information, This will not actually allocate the memory needed for the picture. The code example is as follows:

Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmapfactory.decoderesource (Getresources (), R.drawable.pic1, options); String Text = "ImageHeight:" + options.outheight + ", ImageWidth:" + Options.outwidth + ", ImageType:" + options.outmimetype; Toast.maketext (this, text, Toast.length_long). Show ();

Once you know the size of the picture, you can consider loading the entire picture into memory, or loading the smaller version into memory. Because the size of the phone screen itself is so large, there is no need to load the original size of the picture. For example, if the size of the screen is 128x96, it doesn't make sense to load a picture with a size of 1024x76 in it.

To tell the decoder to compress the picture into memory, you need to set the Insamplesize property of the Bitmapfactory.options object . For example, a picture of 2048*1536, if the value of setting insamplesize is 4. The size of the picture becomes 512x384 when zoomed out. The memory occupied by the original 12M into 0.75M. The following method calculates the Insamplesize value that should be set for a picture:

public static int calculateinsamplesize (bitmapfactory.options options,int reqwidth, int reqheight) {//Reqwidth, Reqheight is the size of the image that you want to display, such as the size of the screen or the size of the ImageView control final int height = options.outheight;final int width = Options.outwidth;int Insamplesize = 1;if (height > Reqheight | | width > reqwidth) {//Description picture's true size, larger than the size you need to display, you need to zoom out final int halfheight = He ight/2;final int halfwidth = width/2;while (halfheight/insamplesize) > reqheight&& (halfwidth/insample Size) > Reqwidth) {insamplesize *= 2;}} return insamplesize;}

With the appropriate insamplesize value, you can actually perform the operation of loading the picture. The code is as follows:

Final Bitmapfactory.options Options = new Bitmapfactory.options (); Options.insamplesize = 2; Options.injustdecodebounds = false; Bitmap Bitmap =  Bitmapfactory.decoderesource (res, resId, options); ImageView ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Imageview.setimagebitmap (bitmap);

In fact for large pictures, by using insamplesize to make the image smaller and loaded into memory, as long as the change is not very small, will not affect the visual effect. But the benefits are obvious and opinion, and will greatly reduce the use of memory. Generally for large-sized photos (such as on the phone itself), setting Insamplesize to 4 generally does not affect the visual effect.

  

Android Learning notes: How to display pictures efficiently, avoid memory overflow and ImageView cannot display large size pictures

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.