1. Image processing is often encountered during Android software development. When converting images to Bitmap, due to the different sizes of the images, when encountering large images, there will be problems beyond memory. In order to solve this problem, the Android API provides this class.
2. Since Android has restrictions on the use of images, if a large picture of several megabytes is loaded, the memory will overflow. Bitmap will load all pixels of the image (i.e. length x width) into memory. If the image resolution is too large, it will directly lead to memory OOM. Only when BitmapFactory loads the image, use the relevant parameters to reduce the loaded pixels.
3. Detailed explanation of relevant parameters:
(1). Value to reduce memory consumption.
For example: the default value ARGB_8888 is changed to RGB_565, saving half of the memory.
(2). Set the scaling ratio and compress large images.
(3). Set and inInputShareable: enable the system to recover memory in time.
A: inPurgeable: When set to True, it means that the system memory can be recycled when it is insufficient. When set to False, it means that it cannot be recycled.
B: inInputShareable: Set whether to copy deeply. Used in combination with inPurgeable. When inPurgeable is false, this parameter has no meaning.
(4). Use decodeStream instead of other methods.
decodeResource, setImageResource, setImageBitmap and other methods
4. Code part:
public static Bitmap getBitmapFromFile(File file, int width, int height) { opts = null; if (null != file && ()) { if (width > 0 && height > 0) { opts = new (); // It just returns the width and height of the image, not a Bitmap object = true; // The information is not saved in bitmap, but in options ((), opts); // Calculate the image scaling ratio final int minSideLength = (width, height); // The thumbnail size is one-something of the original image size. Do it according to business needs. = computeSampleSize(opts, minSideLength, width * height); //Read the picture again, note that the setting has been set back to false at this time = false; // Set whether to copy deeply, use it inPurgeable in combination = true; // When set to True, it means that the system memory can be recycled when it is insufficient. When set to False, it means that it cannot be recycled. = true; } try { return ((), opts); } catch (OutOfMemoryError e) { (); } } return null; }