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.graphics.BitmapFactory;

public class Main {

    public static Bitmap getScreenSizeBitmap(String filePath, int[] screenSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = getSampleSizeAdjustToScreen(filePath, screenSize);
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
        return bitmap;
    }

    public static int getSampleSizeAdjustToScreen(String filePath, int[] screenSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        int w = (int) Math.ceil(options.outWidth / (float) screenSize[0]);
        int h = (int) Math.ceil(options.outHeight / (float) screenSize[1]);

        if (h > 1 || w > 1) {
            if (h > w) {
                options.inSampleSize = h;

            } else {
                options.inSampleSize = w;
            }
        }
        return options.inSampleSize;
    }
}