Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String ImgFolderName = "UDeskIMg";

    public static Uri getOutputMediaFileUri(Context context) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return Uri.fromFile(getOutputMediaFile(context, "IMG_" + timeStamp + ".jpg"));
    }

    public static File getOutputMediaFile(Context context, String mediaName) {
        File mediaStorageDir = null;
        try {
            mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), ImgFolderName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mediaName);
        return mediaFile;
    }
}