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.net.Uri;
import android.os.Environment;

import android.util.Log;

public class Main {
    private final static String TAG = "CameraUtils";

    /**
     * Creates a temporary file for storing a photo that will be taken with the
     * camera.
     * 
     * @param context
     *            the application's context
     * @return a Uri that points to the file
     */
    public static Uri getPhotoUri(Context context) {
        File photo;
        try {
            // place where to store camera taken picture
            photo = createTemporaryFile("picture", ".jpg", context);
            photo.delete();
        } catch (Exception e) {
            Log.v(TAG, "Can't create file to take picture!");
            return null;
        }

        Uri mImageUri = Uri.fromFile(photo);
        return mImageUri;
    }

    /**
     * Creates a temporary file with the specified prefix and extension.
     * 
     * @param part
     *            the prefix for the file
     * @param ext
     *            the extension for the file
     * @param context
     *            the application's context
     * @return the created File
     * @throws Exception
     */
    private static File createTemporaryFile(String part, String ext, Context context) throws Exception {
        File tempDir = Environment.getExternalStorageDirectory();
        tempDir = new File(tempDir.getAbsolutePath() + "/bv-temp/");
        if (!tempDir.exists()) {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }
}