get Bitmap Size from Bitmap file - Android Graphics

Android examples for Graphics:Bitmap File

Description

get Bitmap Size from Bitmap file

Demo Code


//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Build;

import java.io.File;

public class Main {

    public static float[] getBitmapSize(File file) {
        float[] size = new float[2];
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;/*  ww  w. j  a v  a2  s  . c om*/
        BitmapFactory.decodeFile(file.getPath(), opts);

        size[0] = opts.outWidth;
        size[1] = opts.outHeight;
        return size;
    }

    public static int getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
            return bitmap.getByteCount();
        }

        return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
    }
}

Related Tutorials