Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.graphics.Bitmap;
import android.os.Build;

import java.text.DecimalFormat;

public class Main {
    public static final int SIZETYPE_B = 1;
    public static final int SIZETYPE_KB = 2;
    public static final int SIZETYPE_MB = 3;
    public static final int SIZETYPE_GB = 4;

    public static Double getBitmapsize(Bitmap bitmap, int sizeType) {
        long fileS = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            //            fileS = bitmap.getByteCount();
            Bitmap.Config config = bitmap.getConfig();
            if (config == Bitmap.Config.RGB_565 || config == Bitmap.Config.ARGB_4444) {
                fileS = bitmap.getWidth() * bitmap.getHeight() * 2;
            } else if (config == Bitmap.Config.ALPHA_8) {
                fileS = bitmap.getWidth() * bitmap.getHeight();
            } else if (config == Bitmap.Config.ARGB_8888) {
                fileS = bitmap.getWidth() * bitmap.getHeight() * 4;
            }
        } else {
            fileS = bitmap.getRowBytes() * bitmap.getHeight();// Pre HC-MR1
        }

        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
        case SIZETYPE_B:
            fileSizeLong = Double.valueOf(df.format((double) fileS));
            break;
        case SIZETYPE_KB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
            break;
        case SIZETYPE_MB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
            break;
        case SIZETYPE_GB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
            break;
        default:
            break;
        }
        return fileSizeLong;
    }
}