Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {
        /// first avoid allocate memory to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        options.inSampleSize = calcInSampleSizeDuo(options, reqWidth, reqHeight);

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static int calcInSampleSizeDuo(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        while (width / inSampleSize > reqWidth && height / inSampleSize > reqHeight) {
            inSampleSize *= 2;
        }
        return inSampleSize;
    }
}