Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap getSimilarBitmap(File f, int width, int height) {
        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1 = new FileInputStream(f);
            BitmapFactory.decodeStream(stream1, null, o);
            stream1.close();

            float widthScale = o.outWidth / width;
            float heightScale = o.outHeight / height;
            float scale = widthScale > heightScale ? widthScale : heightScale;
            if (scale > 8) {
                o.inSampleSize = 8;
            } else if (scale > 6) {
                o.inSampleSize = 6;
            } else if (scale > 4) {
                o.inSampleSize = 4;
            } else if (scale > 2) {
                o.inSampleSize = 2;
            } else {
                o.inSampleSize = 1;
            }
            o.inJustDecodeBounds = false;
            FileInputStream stream2 = new FileInputStream(f);
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}