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

import android.util.Log;

import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    /**
     * generate new image path file
     *
     * @return
     */
    public static File generateImagePath() {
        try {
            File storageDir = getAlbumDir();
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
            return new File(storageDir, "IMG_" + timeStamp + ".jpeg");
        } catch (Exception e) {
            Log.d("android_utilities", "the error is " + e);
        }
        return null;
    }

    private static File getAlbumDir() {
        File storageDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "Telepathy");
            if (!storageDir.mkdirs()) {
                if (!storageDir.exists()) {
                    Log.d("android_utilities", "failed to create directory");
                    return null;
                }
            }
        } else {
            Log.d("android_utilities", "External storage is not mounted READ/WRITE.");
        }
        return storageDir;
    }
}