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 String savePicture(Bitmap bmp, String file) {
    File filepicture = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    if (file == null || file.equals("")) {
        file = filepicture + "/" + System.currentTimeMillis() + ".jpg";
    }//w ww .j  a  va2 s.com
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return "";
        }
    } finally {
        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    return "file://" + file;
}

From source file:Main.java

public static Uri getOutputMediaFileUri() {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Date");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("Date", "failed to create directory");
            return null;
        }//  w ww . j  a va 2  s. co  m
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return Uri.fromFile(mediaFile);
}

From source file:Main.java

private static File getAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            albumName);/*w ww.  j a  v  a 2s .  com*/
    if (file.exists()) {
        return file;
    }
    if (!file.mkdirs()) {
        Log.e(TAG, "Directory not created");
    }
    return file;
}

From source file:Main.java

/**
 * Get Image Uri when clicked from Camera
 *
 * @return Uri of clicked Image//from w w w . j  a  va 2s.  c o  m
 */
public static Uri getBitmapUri() {
    File imageStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CRMnextImages");
    if (!imageStorageDir.exists()) {
        imageStorageDir.mkdirs();
    }
    File file = null;
    try {
        file = File.createTempFile("IMG_" + String.valueOf(System.currentTimeMillis()), ".jpg",
                imageStorageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Uri.fromFile(file);
}

From source file:Main.java

public static void writeToPublicDirectory(String filename, byte[] data, String directory,
        String environmentDirectory) throws Exception {
    File publicDirectory = new File(Environment.getExternalStoragePublicDirectory(environmentDirectory),
            directory);//from   www . j  ava  2 s.c  o  m
    boolean result = publicDirectory.mkdirs();
    File targetFile = new File(publicDirectory, filename);
    FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
    fileOutputStream.write(data);
    fileOutputStream.close();
}

From source file:Main.java

/**
 * Determine the camera folder. There seems to be no Android API to work for real devices, so this is a best guess.
 *
 * @return the default camera folder./* ww w.  j ava 2  s .  c  o m*/
 */
public static String getDefaultCameraFolder() {
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (path.exists()) {
        File test1 = new File(path, "Camera/");
        if (test1.exists()) {
            path = test1;
        } else {
            File test2 = new File(path, "100ANDRO/");
            if (test2.exists()) {
                path = test2;
            } else {
                path = new File(path, "100MEDIA/");
            }
        }
    } else {
        path = new File(path, "Camera/");
    }
    return path.getAbsolutePath();
}

From source file:Main.java

public static File createFolders() {
    File baseDir;/*from  w w  w.j a  v a 2s . com*/
    if (android.os.Build.VERSION.SDK_INT < 8) {
        baseDir = Environment.getExternalStorageDirectory();
    } else {
        baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    }
    if (baseDir == null)
        return Environment.getExternalStorageDirectory();
    File aviaryFolder = new File(baseDir, FOLDER_NAME);
    if (aviaryFolder.exists())
        return aviaryFolder;
    if (aviaryFolder.isFile())
        aviaryFolder.delete();
    if (aviaryFolder.mkdirs())
        return aviaryFolder;
    return Environment.getExternalStorageDirectory();
}

From source file:Main.java

public static String getDataDirectoryLocation() {
    return (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
}

From source file:Main.java

public static Uri getOutputImageFileURL() {
    Log.d(LOG_MESSAGE, "external dir:"
            + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(LOG_MESSAGE, "failed to create directory");
            return null;
        }/*from w w  w  . j  av  a2 s . c o m*/
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + "IMG_" + timeStamp + ".png");
    return Uri.fromFile(mediaFile);
}

From source file:Main.java

public static File getNewImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName, ".jpg", storageDir);
}