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

@SuppressLint("SimpleDateFormat")
public static File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, /* prefix */
            JPEG_EXTENSION, /* suffix */
            storageDir /* directory */
    );/*from w w  w  .  ja va  2 s.c  o m*/
    return image;
}

From source file:Main.java

/**
 * Create an default file for save image from camera.
 *
 * @return/* w w  w . j  a  va2s.  c  o m*/
 * @throws IOException
 */
public static File createDefaultImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + ".jpg";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }
    return new File(storageDir, imageFileName);
}

From source file:Main.java

public static String getDir(Context context, String path) {
    //      File dir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    path = dir.getAbsolutePath() + "/" + path;
    return path;//from w  ww  .  j av  a  2 s .  c  o  m
}

From source file:Main.java

/** Create a File for saving an image*/
private static File getOutputMediaFile(String fileName) {
    // 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), "DFCarChecker");

    // 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("DFCarChecker", "failed to create directory");
            return null;
        }/*from www  .j a v a2  s .c  o  m*/
    }

    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + fileName + ".jpg");

    return mediaFile;
}

From source file:Main.java

public static Uri createImageFile() {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image;//from www . j  a  va  2  s . co m
    try {
        image = File.createTempFile(imageFileName, /* prefix */
                ".jpg", /* suffix */
                storageDir /* directory */
        );
    } catch (IOException e) {
        Log.d(TAG, "create tmp file error!", e);
        return null;
    }
    return Uri.fromFile(image);
}

From source file:Main.java

/**
 * Create a File for saving an image or video
 *//*from   w ww  . j  a v  a  2  s  .co m*/
public 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;
        }
    }
    // 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 if (type == MEDIA_TYPE_AUDIO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "AUD_" + timeStamp + ".mp3");
    } else {
        return null;
    }
    return mediaFile;
}

From source file:Main.java

/**
 * Returns the music cache directory./*w  w w .  j  av  a2s  . c om*/
 * @return Music cache directory
 */
public static File getMusicCacheDir() {
    File music = Environment.getExternalStoragePublicDirectory("SismicsMusic");
    File cache = new File(music, "cache");
    if (!cache.exists()) {
        cache.mkdirs();
    }
    return cache;
}

From source file:Main.java

private static File getOutputMediaFile(int type) {

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("failed", "Oops! Failed create " + "Alumni Space Picture" + " directory");
            return null;
        }//  ww  w.java 2  s .  c  o m
    }

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

    return mediaFile;

}

From source file:Main.java

/**
 * Create a backup of the database.//from   ww w. j a v a 2 s .  c  o m
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void exportDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Create a backup of the database.//from  www . ja v  a2s  .c  om
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void importDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank von Downloads importiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Import fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}