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

/** Create a File for saving an image or video */
private static File getOutputMediaFile() {
    // 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), "FoodBook");
    // 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("FoodBook", "failed to create directory");
            return null;
        }// ww w .  j  a  va 2 s  . c o m
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

From source file:Main.java

/** Create a File for saving an image or video */
public static File getOutputMediaFile(int type) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        return null;

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MacauFood");
    // 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("MacauFood", "failed to create directory");
            return null;
        }//from  w  w  w . j a  v  a  2 s.c om
    }

    // 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

/**
 * Get the External Downloads Directory to 
 * store the Selfies.//  w w  w . ja va  2s  .  c  om
 * 
 * @param ghostmyselfieName
 */
public static File getGhostMySelfieStorageDir(String ghostmyselfieName) {
    //Check to see if external SDCard is mounted or not.
    if (isExternalStorageWritable()) {
        // Create a path where we will place our selfie in the
        // user's public Downloads directory. Note that you should be
        // careful about what you place here, since the user often 
        // manages these files.
        final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        final File file = new File(path, ghostmyselfieName);
        // Make sure the Downloads directory exists.
        path.mkdirs();
        return file;
    } else {
        return null;
    }
}

From source file:Main.java

public static File getAlbumDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File storageDir = new File(dir, "MyAlbum");

        if (!storageDir.mkdirs()) {
            if (!storageDir.exists()) {
                return null;
            }/*from w w w  . jav a  2s  .  c  o m*/
        }

        return storageDir;

    } else {
        return null;
    }
}

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;//ww  w.ja  va2 s .co m
    }

    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;
    }

    //USAGE:
    //        fileUri = Helpers.getOutputMediaFileUri(
    //                        MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE,
    //                           Helpers.getApplicationName(this)
    //                         ); // create a file to save the image

    return mediaFile;
}

From source file:Main.java

public static File createTmpFile(Context context) throws IOException {
    File dir = null;/*from w  ww  .  j  a v  a 2 s. co  m*/
    if (TextUtils.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)) {
        dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        if (!dir.exists()) {
            dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
            if (!dir.exists()) {
                dir = getCacheDirectory(context, true);
            }
        }
    } else {
        dir = getCacheDirectory(context, true);
    }
    return File.createTempFile(JPEG_FILE_PREFIX, JPEG_FILE_SUFFIX, dir);
}

From source file:Main.java

/**
 * Create a File for saving an image or video
 *///from w w w  . j av a 2  s  . c om
@SuppressLint("SimpleDateFormat")
public static File getOutputMediaFile(int type, Context context) {
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = null;
    if (type == MEDIA_TYPE_IMAGE) {
        File mediaStorageDir = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "StoryCapture");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("StoryCapture", "failed to create directory");
                return null;
            }
        }
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_AUDIO) {
        String fileName = "AUDIO_" + timeStamp + ".3gp";
        mediaFile = new File(context.getFilesDir(), fileName);
    }
    return mediaFile;
}

From source file:Main.java

public static File getAlbumStorageDir() {
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), FOLDER);
    return file;//  ww  w  .  java  2  s .c o m
}

From source file:Main.java

public static Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;// w  ww . j  av  a 2 s.  c  o  m
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

From source file:Main.java

/**
 * Creates a thumbnail file starting from an image/video file
 *
 * @param original_file original file path
 * @param video         true if the file is a video. false if the file is a picture
 * @return a file/*ww w.ja  v a2  s. co  m*/
 */
private static File generateThumbnailFileForFile(File original_file, boolean video) {
    // Create an image file name
    try {
        String imageFileName = "thumb" + original_file.getName();
        Log.d(TAG, original_file.getName());
        File storageDir = Environment.getExternalStoragePublicDirectory(
                video ? Environment.DIRECTORY_MOVIES : Environment.DIRECTORY_PICTURES);
        return File.createTempFile(imageFileName, /* prefix */
                THUMB_FORMAT_EXTENSION, /* suffix */
                storageDir /* directory */
        );
    } catch (IOException e) {
        Log.d(TAG, "Error", e);
    }
    return null;

}