Example usage for android.os Environment getExternalStoragePublicDirectory

List of usage examples for android.os Environment getExternalStoragePublicDirectory

Introduction

In this page you can find the example usage for android.os Environment getExternalStoragePublicDirectory.

Prototype

public static File getExternalStoragePublicDirectory(String type) 

Source Link

Document

Get a top-level shared/external storage directory for placing files of a particular type.

Usage

From source file:Main.java

public static File getArtworkStorageDir() {
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
            ARTWORKS_DIR_NAME);/*  www.j av a  2 s  .  c o m*/
    if (!file.mkdirs()) {
        Log.e(TAG, "Directory not created");
    }
    return file;
}

From source file:Main.java

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }/*w  ww  . ja v a  2 s.c o  m*/
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:Main.java

private static File getAlbumDir() {
    File storageDir = null;//from   w  w w  .j  a v a  2  s  . c  om
    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;
}

From source file:Main.java

public static void writeToPublicDirectory(String filename, String string, String directory,
        String environmentDirectory) throws Exception {
    File publicDirectory = new File(Environment.getExternalStoragePublicDirectory(environmentDirectory),
            directory);/*from w ww.j  ava 2s.c o  m*/
    boolean result = publicDirectory.mkdirs();
    File file = new File(publicDirectory, filename);
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
    outputStreamWriter.write(string);
    outputStreamWriter.close();
}

From source file:Main.java

/** 
 * Create a file Uri for saving a recorded selfie
 *///from   ww  w .  ja  v  a 2 s .  c o m
@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;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.FROYO)
private static File getDownloadDir_Froyo() {
    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}

From source file:Main.java

/**
 * @param context the context/*from   w ww. j a  v  a2 s.c o  m*/
 * @param filename the filename
 * @return true if a file named "filename" is stored in the downloads directory
 */
public static Boolean doesFileExistInDownloads(Context context, String filename) {
    File dstDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    if (dstDir != null) {
        dstDir.mkdirs();
    }

    File dstFile = new File(dstDir, filename);
    return dstFile.exists();
}

From source file:Main.java

/** Create a File for saving an image or video specific to an app name */
public static File getOutputMediaFile(int type, String optionalAppName) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    String appname = "CameraApp";
    if (optionalAppName != null && !optionalAppName.isEmpty()) {
        appname = optionalAppName;/*from w  ww. j  a  va  2s. c  om*/
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appname);
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

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

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE) {
        mediaFile = new File(
                mediaStorageDir.getPath() + File.separator + "IMG_" + appname + timeStamp + ".jpg");
    } else if (type == MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) {
        mediaFile = new File(
                mediaStorageDir.getPath() + File.separator + "VID_" + appname + timeStamp + ".mp4");
    } else {
        return null;
    }
    return mediaFile;
}

From source file:Main.java

public static String getPhotoSavePath(String dir) {
    String dcimDir = null;/*from   ww w. ja v a  2s  . c om*/

    File extDcimDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (extDcimDir == null || !extDcimDir.exists()) {
        File extDir = Environment.getExternalStorageDirectory();
        if (extDir == null) {
            return null;
        }
        dcimDir = extDir.getAbsolutePath() + "/DCIM";
        try {
            new File(dcimDir).mkdirs();
        } catch (Exception e) {
        }
    } else {
        dcimDir = extDcimDir.getAbsolutePath();
    }
    if (dir == null) {
        return dcimDir + PHOTO_FOLDER;
    } else {
        return dcimDir + "/" + dir;
    }
}

From source file:Main.java

public static File getOutputMediaFile(final int type) {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Dove");

    //.delete();/* w ww.  j  av a2s .  c o m*/
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}