Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;

public class Main {
    public static Bitmap getTempBitmap(Context context) {
        Bitmap capturedBitmap = null;
        final File file = getTempFile(context);
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;

            AssetFileDescriptor fileDescriptor = context.getContentResolver()
                    .openAssetFileDescriptor(Uri.fromFile(file), "r");
            capturedBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return capturedBitmap;
    }

    public static File getTempFile(Context context) {
        //It will return /sdcard/image.tmp
        final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName());
        if (!path.exists()) {
            path.mkdir();
        }
        return new File(path, "image.tmp");
    }
}