Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;

public class Main {
    /** 
     * Create a file Uri for saving a recorded selfie
     */
    @SuppressLint("SimpleDateFormat")
    public static Uri getRecordedGhostMySelfieUri(Context context) {

        // Check to see if external SDCard is mounted or not.
        if (isExternalStorageWritable()) {
            // Create a path where we will place our recorded selfie in
            // the user's public DCIM directory. Note that you should
            // be careful about what you place here, since the user
            // often manages these files.
            final File ghostmyselfieStorageDir = Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

            // Create the storage directory if it does not exist
            if (!ghostmyselfieStorageDir.exists()) {
                if (!ghostmyselfieStorageDir.mkdirs()) {
                    return null;
                }
            }

            // Create a TimeStamp for the selfie file.
            final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            // Create a selfie file name from the TimeStamp.
            final File ghostmyselfieFile = new File(
                    ghostmyselfieStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

            // Always notify the MediaScanners after storing
            // the GhostMySelfie, so that it is immediately available to
            // the user.
            notifyMediaScanners(context, ghostmyselfieFile);

            //Return Uri from GhostMySelfie file.
            return Uri.fromFile(ghostmyselfieFile);

        } else
            //Return null if no SDCard is mounted.
            return null;
    }

    /**
     * Checks if external storage is available for read and write.
     * 
     * @return True-If the external storage is writable.
     */
    private static boolean isExternalStorageWritable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    /**
     * Notifies the MediaScanners after Downloading the GhostMySelfie, so it
     * is immediately available to the user.
     */
    public static void notifyMediaScanners(Context context, File ghostmyselfieFile) {
        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context, new String[] { ghostmyselfieFile.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                    }
                });
    }
}