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;

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

public class Main {
    public static Bitmap getBitmapFromFile(File file, int w) {
        FileInputStream fileInputStream = null;
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            if (file == null || !file.exists()) {
                return null;
            } else if (file.length() == 0) {
                file.delete();
                return null;
            }
            fileInputStream = new FileInputStream(file);
            BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
            int be = getSampleSize(opts.outWidth, w);
            opts.inSampleSize = be;
            opts.inJustDecodeBounds = false;
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            return BitmapFactory.decodeStream(fileInputStream, null, opts);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static int getSampleSize(float size, float outSize) {
        int be = (int) (size / outSize);
        if (be <= 0) {
            be = 1;
        }
        return be;

    }
}