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

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 *///from   w  w w  . jav  a 2  s. c om
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraSample");
    // 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("CameraSample", "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_SENSOR) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".dat");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:com.abid_mujtaba.bitcoin.tracker.data.Data.java

private static File data_file() // Method for getting File object handle on the local data file
{
    File dir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                    + "/" + FOLDER);
    dir.mkdirs(); // Make the directory along with all necessary parent directories if they don't already exist

    return new File(dir, FILENAME);
}

From source file:org.onebusaway.android.io.backup.Backup.java

private static File getBackup(Context context) {
    File backupDir = Environment.getExternalStoragePublicDirectory(BACKUP_TYPE);
    return new File(backupDir, BACKUP_NAME);
}

From source file:MainActivity.java

private Uri createFileURI() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis());
    String fileName = "PHOTO_" + timeStamp + ".jpg";
    return Uri.fromFile(
            new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName));
}

From source file:Main.java

public static File getOutputMediaFile(int type, int location) {

    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }/*from w  w w. j  ava 2s. c  o  m*/

    File mediaStorageDir = (location == STORAGE_INT)
            ? new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    STR_APP_NAME)
            : getActualExtSDCard(STR_APP_NAME);
    // 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(TAG, "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;
}

From source file:can.yrt.onebusaway.backup.Backup.java

@TargetApi(8)
private static File getBackup(Context context) {
    if (!isBackupEnabled()) {
        throw new RuntimeException("Backup isn't enabled!");
    }/*  w ww  .j  a v  a  2  s  . co m*/
    File backupDir = Environment.getExternalStoragePublicDirectory(BACKUP_TYPE);
    return new File(backupDir, BACKUP_NAME);
}

From source file:Main.java

/**
 * Copy a file into a dstPath directory.
 * The output filename can be provided./*from   w ww .j a v a 2 s . c o m*/
 * The output file is not overriden if it is already exist.
 * @param context the context
 * @param sourceFile the file source path
 * @param dstDirPath the dst path
 * @param outputFilename optional the output filename
 * @return the downloads file path if the file exists or has been properly saved
 */
public static String saveFileInto(Context context, File sourceFile, String dstDirPath, String outputFilename) {
    // sanity check
    if ((null == sourceFile) || (null == dstDirPath)) {
        return null;
    }

    // defines another name for the external media
    String dstFileName;

    // build a filename is not provided
    if (null == outputFilename) {
        // extract the file extension from the uri
        int dotPos = sourceFile.getName().lastIndexOf(".");

        String fileExt = "";
        if (dotPos > 0) {
            fileExt = sourceFile.getName().substring(dotPos);
        }

        dstFileName = "MatrixConsole_" + System.currentTimeMillis() + fileExt;
    } else {
        dstFileName = outputFilename;
    }

    File dstDir = Environment.getExternalStoragePublicDirectory(dstDirPath);
    if (dstDir != null) {
        dstDir.mkdirs();
    }

    File dstFile = new File(dstDir, dstFileName);

    // Copy source file to destination
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        // create only the
        if (!dstFile.exists()) {
            dstFile.createNewFile();

            inputStream = new FileInputStream(sourceFile);
            outputStream = new FileOutputStream(dstFile);

            byte[] buffer = new byte[1024 * 10];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
        }
    } catch (Exception e) {
        dstFile = null;
    } finally {
        // Close resources
        try {
            if (inputStream != null)
                inputStream.close();
            if (outputStream != null)
                outputStream.close();
        } catch (Exception e) {
        }
    }

    if (null != dstFile) {
        return dstFile.getAbsolutePath();
    } else {
        return null;
    }
}

From source file:com.qasp.diego.arsp.Atualiza.java

public static boolean DownloadFTP() throws IOException {

    final String FTPURL = "37.187.45.24";
    final String USUARIO = "diego";
    final String SENHA = "Jogador5";
    final String ARQUIVO = "data.dat";
    FTPClient ftp = new FTPClient();

    try {/*from w ww  . j  av  a  2 s.  c  o m*/
        ftp.connect(FTPURL);
        ftp.login(USUARIO, SENHA);
        ftp.changeWorkingDirectory("/httpdocs/tcc/TCCpython");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        OutputStream outputStream = null;
        boolean downloadcomsucesso = false;
        try {
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    ARQUIVO);
            f.createNewFile();
            outputStream = new BufferedOutputStream(new FileOutputStream(f));
            downloadcomsucesso = ftp.retrieveFile(ARQUIVO, outputStream);
        } finally {
            if (outputStream != null)
                outputStream.close();
        }
        return downloadcomsucesso;
    } finally {
        if (ftp != null) {
            ftp.logout();
            ftp.disconnect();
        }
    }
}

From source file:my.extensions.app.AudioFileLister.java

private String listFiles() {
    if (musicDirectoryReadable()) {
        File audioDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

        Iterator<File> fileIterator = FileUtils.iterateFiles(audioDir, FILE_EXTS, true);

        List<FileInfo> files = new ArrayList<FileInfo>();

        File f;//w  ww  .j a v a  2 s. c  o m
        while (fileIterator.hasNext()) {
            f = fileIterator.next();
            files.add(new FileInfo(f));
        }

        String filesJson = gson.toJson(files);

        return "{\"success\": true, \"files\": " + filesJson + "}";
    } else {
        return "{\"success\": false, \"error\":\"audio directory not readable\"}";
    }
}

From source file:com.google.android.apps.forscience.whistlepunk.PictureUtils.java

public static File createImageFile(long timestamp) throws IOException {
    // Create an image file name
    String imageDate = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(timestamp));
    String imageFileName = String.format(PICTURE_NAME_TEMPLATE, imageDate);
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            PICTURES_DIR_NAME);/*from  w ww  .  j  a v  a 2 s  .  c o m*/
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }
    File imageFile = new File(storageDir, imageFileName);
    return imageFile;
}