compute Bitmap In Memory Size - Android Graphics

Android examples for Graphics:Bitmap Size

Description

compute Bitmap In Memory Size

Demo Code


//package com.java2s;
import java.io.File;

import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

public class Main {
    public static int computeInMemorySize(File path, Config preferredConfig) {
        Options op = new Options();
        op.inInputShareable = true;/*from  w  w  w. j  av a2 s.  c o m*/
        if (preferredConfig != null)
            op.inPreferredConfig = preferredConfig;

        BitmapFactory.decodeFile(path.getAbsolutePath(), op);

        int bpp;
        switch (op.inPreferredConfig) {
        case ALPHA_8:
            bpp = 1;
            break;
        case ARGB_4444:
        case RGB_565:
            bpp = 2;
            break;
        default:
            bpp = 4;
            break;
        }

        return op.outWidth * op.outHeight * bpp;
    }
}

Related Tutorials