Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Android memory optimization of the optimal load Bitmap     - Getting Started with Linux system to learn: how to configure a static IP address for CentOS7 (Linux)

- GNU Linux use diff to generate a patch with the patch (Linux)

- Linux kernel panic (because glibc result) Repair (Linux)

- Linux Shell Scripting Interview Question (Linux)

- Elaborate .NET Multithreading: Thread Pool (Programming)

- Several reasons MySQL garbled (Database)

- JSON Introduction and Usage Summary (Programming)

- SUSE Linux network configuration and firewall configuration (Linux)

- CentOS / Linux NIC set IP address configuration (Linux)

- Sublime Text 3 (Linux)

- Installation and use the telnet command under CentOS 6.x (Linux)

- Java multi-threaded communications pipeline flow (Programming)

- Those things packaged using Gradle to Android (Programming)

- Linux (RHEL6 CENTOS6 OLE6) VNC-SERVER Installation and Configuration (Server)

- The Gitlab migrated to Docker container (Server)

- Java data structures - the single linked list LinkedList linear table (Programming)

- To teach you how to safely send mail (Linux)

- impdp error ORA-39001, ORA-39000, ORA-31619 (Database)

- Linux iptables: basic principles and rules (Linux)

- Hazelcast integration with MongoDB (Database)

  Android memory optimization of the optimal load Bitmap
  Add Date : 2018-11-21      
  An article on the Android Bitmap memory footprint calculation and loading considerations, wrote Bitmap Bitmap basics and use the precautions you need to know, I will write in this Android app Bitmap creation and loading.

BitmapFactory use:

When it comes to image loading must say BitmapFactory, see the name to know his role, is a production Bitmap factory, below is some of the factory method:

See BitmapFactory can use the array to store data Bitmap, Bitmap resource ID, Bitmap files as a data source to create a Bitmap object, look at the specific circumstances of your data source program which is provided. These methods for each data source provides two methods, there is need to look at BitmapFacotry.Options argument, which is very important, because he was able to greatly reduce your consumption of memory.

BitmapFacotry.Options of inJustDecodeBounds parameters:

To conserve memory, in many cases to go through the original image scaling processing, according to the size of the control handle to the corresponding size of the picture, then use BitmapFactory create Bitmap, in many cases will use the following code:

BitmapFactory.Options options = new BitmapFactory.Options ();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource (getResources (), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

Note that in the above options.inJustDecodeBounds = true inJustDecodeBounds parameters, in order to avoid my translation is inaccurate here I first posted the original google:

If set to true, the decoder will return null (no bitmap), but the out ... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

I will say it is not to the pixel area of the bitmap memory allocation at decode time, in addition to other information this distinction you can get to the Bitmap. So there is a lot of sense, you have neither consume memory and get a picture of information for you to help deal with the next image.

BitmapFacotry.Options of inSampleSize parameters:

Back You have to get the original picture size, the next step is to take the picture scaled to the size you need, can be set by inSampleSize parameters, google original explanation:

If set to a value> 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width / height of the original, and 1/16 the number of pixels Any value < = 1 is treated the same as 1. Note:. the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. also, powers of 2 are often faster / easier for the decoder to honor. (no English documents Kan Bukan I still want to google the original stickers, my English is rotten, the translation may not be accurate)

This probably means is that you can adjust the memory parameters when you needed to decode the original image, a bit like sampling rate, will lose some of the pixel value is greater than 1, when a power of 2 is more conducive to operations. For example: When inSampleSize == will return 4:00 a size (length and width) is 1/4 of its original size, the pixel is 1/16 of the original picture. This value is how to calculate it?

public static int calculateInSampleSize (
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    finalint height = options.outHeight;
    finalint width = options.outWidth;
    int inSampleSize = 1;

    if (height> reqHeight || width> reqWidth) {

        finalint halfHeight = height / 2;
        finalint halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // Height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize)> reqHeight
                && (HalfWidth / inSampleSize)> reqWidth) {
            inSampleSize * = 2;

    return inSampleSize;

In the first set, when decode options.inJustDecodeBounds = true, and then get to the picture parameter set to false, this is the skill decode time, put the following complete code stickers can be used as a tool method:

public static Bitmap decodeSampledBitmapFromResource (Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds = true to check dimensions
    finalBitmapFactory.Options options = newBitmapFactory.Options ();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource (res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize (options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    returnBitmapFactory.decodeResource (res, resId, options);

The above method from google official website, no need to make changes, this is the programmer ism it, the key is to know why so write. Here is a method I wrote can be directly used as tools.

    / **
    * For image compression, mainly to solve the control to display image memory for causing too much OOM problem, the general compressed image size and its control should be used to display a similar size.
    * @param Context Context
    * @param ResId image asset Id
    * @param ReqWidth desired width compression
    * @param ReqHeight desired compression height
    * @return Compressed image
    * /
    public static Bitmap compressBitmapFromResourse (Context context, int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options ();
        / *
        * When the first analysis, inJustDecodeBounds set to true,
        * Do not allocate memory for the bitmap, although bitmap return value is empty, but you can get the image size
        * /
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource (context.getResources (), resId, options);

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height> reqHeight || width> reqWidth) {
            final int heightRatio = Math.round ((float) height / (float) reqHeight);
            final int widthRatio = Math.round ((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio heightRatio: widthRatio;?
        options.inSampleSize = inSampleSize;
        inSampleSize using the calculated values // parsing picture again
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource (context.getResources (), resId, options);

These are loaded into memory Bitmap some tips on Android, we are not in the future be able to apply it well, because to avoid loading image caused such a problem OOM it? In addition to technical image loading, image cache for an application is also very important, the next article will explain the Android memory optimization of the memory cache.
- Android engineers interview questions (Programming)
- Android judgment toward camera pictures (Programming)
- HBase in MVCC implementation mechanism and its application (Database)
- Linux environment Duplex (multi-machine) automatic mutual backup scheme (Linux)
- Linux modify the system time (Linux)
- Oracle 10g after the failure to start the upgrade (Oracle instance terminated. Disconnection forced) (Database)
- Android and OpenCV2.4.4 build an image processing framework (2013 edition) (Linux)
- Linux virtual memory and physical memory (Linux)
- JBPM6 Installation and Configuration Tutorial (Linux)
- Linux IO redirection (Linux)
- Explore Android SQLite3 multithreading (Programming)
- MySQL 5.6 database code changes (Database)
- Custom Android UI template Comments (Programming)
- sqlplus connect to Oracle (Database)
- MySQL partition table Comments (Database)
- CentOS 6.5 dual card configuration, one of the external network, a local area network connection (Linux)
- Git common skills (Linux)
- tespeed - test speed of Python tools (Linux)
- Linux Network Programming - raw socket instance: MAC header message analysis (Programming)
- CentOS 7 How to install MySQL Server (Database)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.