generate Timestamp based Photo File Uri - Android Camera

Android examples for Camera:Photo

Description

generate Timestamp based Photo File Uri

Demo Code


//package com.java2s;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static Uri generateTimestampPhotoFileUri() {
        File photoFile = null;/*from   w ww .java 2  s .  c om*/
        Uri photoFileUri = null;

        File outputDir = getPhotoDirectory();

        if (outputDir != null) {
            String timeStamp = new SimpleDateFormat("yyyyMMDD_HHmmss")
                    .format(new Date());
            String photoFileName = "IMG_" + timeStamp + ".jpg";

            photoFile = new File(outputDir, photoFileName);
            photoFileUri = Uri.fromFile(photoFile);
        }

        return photoFileUri;
    }

    public static File getPhotoDirectory() {
        File outputDir = null;
        String externalStorageState = Environment.getExternalStorageState();

        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
            File picturesDir = Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            outputDir = new File(picturesDir, "asdf");

            if (!outputDir.exists()) {
                if (!outputDir.mkdirs()) {
                    Log.e("CAMERA_HELPER",
                            "Unable to create output directory");
                    outputDir = null;
                }
            }
        }
        return outputDir;
    }
}

Related Tutorials