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 java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class Main {
    private static final int MAX_SIZE = 200;

    public static File downSample(Context context, Uri uri) throws Exception {
        Bitmap b = null;

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        int scale = 1;
        if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
            scale = (int) Math.pow(2, (int) Math
                    .round(Math.log(MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        InputStream is = context.getContentResolver().openInputStream(uri);
        b = BitmapFactory.decodeStream(is, null, o2);
        is.close();

        File outputDir = context.getCacheDir();
        File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
        FileOutputStream fos = new FileOutputStream(outputFile);
        b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
        fos.close();

        return outputFile;
    }
}