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

/**
 * Construct a File for an xxxx.jpg image in the external storage directory
 * /*from   w  ww.  ja  v  a  2s.c o m*/
 * @param name
 *            the 'xxxx'
 * @return File
 */
public static File constructExternalImageFile(String name) {
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File imageFile = new File(storageDir, name + JPEG_EXTENSION);
    imageFile = imageFile.getAbsoluteFile();
    return imageFile;
}

From source file:Main.java

public static File buildImageFilePath(String filename) throws IOException {

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = filename + "_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );//from  w w w. ja  va 2  s  .  c  om

    return image;
}

From source file:Main.java

public static File getOutputMediaFile(int type, String folderName) {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(folderName, "failed to create directory");
            return null;
        }/*ww  w.  ja  va2 s .c  om*/
    }
    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 + ".3gp");
    } else {
        return null;
    }
    return mediaFile;
}

From source file:Main.java

public static File getPhotoDir() {
    File picDirectory = null;/*from   www. j  av a2  s . co  m*/
    File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    picDirectory = new File(pictureDir, "Jing");
    if (!picDirectory.exists()) {
        if (!picDirectory.mkdirs()) {
            picDirectory = null;
        }
    }
    return picDirectory;
}

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);
    return File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );/*from www.j  a v a2 s  . c om*/
}

From source file:Main.java

/** Create a File for saving an image or video */
public 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), APPDIR);
    // 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(APPDIR, "failed to create directory");
            return null;
        }/*from  ww  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;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    return mediaFile;
}

From source file:Main.java

/**
 * Get the corresponding file of the dcimDirectory /sdcard/DCIM/name. Will
 * create the dcimDirectory if is doesn't exist.
 * /*from   w w w.  j a  v a2s.  c o m*/
 * @param dirName
 *            the dcimDirectory dirName.
 * @return the dcimDirectory's corresponding file.
 */
public static File getDcimDirectory(String dirName) {
    if (dirName == null) {
        return null;
    }

    File dcimDirectory = null;
    String dcimPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
    dcimDirectory = new File(dcimPath, dirName);
    if (!dcimDirectory.exists()) {
        dcimDirectory.mkdirs();
    }

    return dcimDirectory;
}

From source file:Main.java

/**
 * Retrieves FreeFlight media directory.
 * May return null.//from   w ww  .ja  v  a  2  s .c o m
 * @param context
 * @return Media directory to store the media files or null if sd card is not mounted.
 */
public static File getMediaFolder(Context context) {
    File dcimFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

    if (dcimFolder == null) {
        Log.w(TAG, "Looks like sd card is not available.");
        return null;
    }

    File mediaFolder = new File(dcimFolder, MEDIA_PUBLIC_FOLDER_NAME);

    if (!mediaFolder.exists()) {
        mediaFolder.mkdirs();
        Log.d(TAG, "Root media folder created " + mediaFolder);
    }

    return mediaFolder;
}

From source file:Main.java

public static File getOutputMediaFile() {

    // External sdcard location

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);/*from  w ww.  j  a  v  a 2s . com*/
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;

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

    return mediaFile;
}

From source file:Main.java

public static Bitmap saveBitmapToFile(Activity activity, Bitmap bitmap) {
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File imageFile = new File(mPath);
    boolean create = imageFile.mkdirs();
    boolean canWrite = imageFile.canWrite();
    Calendar cal = Calendar.getInstance();
    String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);

    String filename = null;/* w ww  .j  a  v  a 2 s.com*/
    int i = 0;
    while (imageFile.exists()) {
        i++;
        filename = date + "_mandelbrot" + i + ".png";
        imageFile = new File(mPath, filename);
        boolean canWrite2 = imageFile.canWrite();

    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /*resultB*/bitmap.compress(CompressFormat.PNG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(imageFile));
        activity.sendBroadcast(intent);

        displaySuccesToast(activity);
    } catch (FileNotFoundException e) {
        displayFileError(activity);
        e.printStackTrace();
    } catch (IOException e) {
        displayFileError(activity);
        e.printStackTrace();
    }
    return bitmap;
}