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;

import android.util.Log;

public class Main {
    private static final String TAG = "UtilFuncs";
    private static boolean enableLog = true;

    public static Bitmap decodeFile(String path, int desWidth, int desHeight) {
        Bitmap result = null;
        File f = null;
        FileInputStream fileInputStream = null;
        try {
            f = new File(path);
            if (!f.exists()) {
                return null;
            }
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            fileInputStream = new FileInputStream(f);
            BitmapFactory.decodeStream(fileInputStream, null, o);

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < desWidth || height_tmp / 2 < desHeight)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            result = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
            logE(TAG, "error:" + e.getStackTrace());
        } finally {
            if (f != null) {
                f = null;
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fileInputStream = null;
            }
        }
        return result;
    }

    public static void logE(String tag, String msg) {
        if (enableLog) {
            Log.e(tag, msg);
        }
    }
}