get Output Media File from external storage - Android android.os

Android examples for android.os:External Storage

Description

get Output Media File from external storage

Demo Code

import android.graphics.Color;
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 File getOutputMediaFile(String appName) {
        // check if sd card is mounted
        if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) {
            Log.e("MainActivity", "External Media is not mounted");
        }/*from   w w  w  . j  a va2s.c  o  m*/

        File mediaDirectory = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                appName);

        // Create the storage directory if it does not exist
        if (!mediaDirectory.exists()) {
            if (!mediaDirectory.mkdirs()) {
                Log.e("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;

        mediaFile = new File(mediaDirectory.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }

}

Related Tutorials