Example usage for android.os Environment getExternalStorageDirectory

List of usage examples for android.os Environment getExternalStorageDirectory

Introduction

In this page you can find the example usage for android.os Environment getExternalStorageDirectory.

Prototype

public static File getExternalStorageDirectory() 

Source Link

Document

Return the primary shared/external storage directory.

Usage

From source file:Main.java

public static String getLogFile() {
    // TODO Auto-generated method stub
    return Environment.getExternalStorageDirectory() + File.separator + LOGS_FOLDER + File.separator
            + "log4j.txt";
}

From source file:Main.java

public static File newFile() {
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd_HH-mm-ss", Locale.getDefault());
    String filename = dateFormat.format(date) + ".jpg";
    return new File(
            Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + filename);
}

From source file:Main.java

public static void DownloadFile(String u) {
    try {/* w w w  .  ja v  a 2s.  c om*/
        Logd(TAG, "Starting download of: " + u);
        URL url = new URL(u);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        //File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        checkStorageDir();
        File storageDir = new File(
                Environment.getExternalStorageDirectory() + "/Android/data/com.nowsci.odm/.storage");
        File file = new File(storageDir, getFileName(u));
        Logd(TAG, "Storage directory: " + storageDir.toString());
        Logd(TAG, "File name: " + file.toString());
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
        Logd(TAG, "File written");
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider  
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider  
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*  w w w  .j a  va  2s .  co  m*/

            // TODO handle non-primary volumes  
        }
        // DownloadsProvider  
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider  
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)  
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address  
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File  
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:com.itbeyond.common.EOTrackMe.java

public static File getLogFile() {
    File gpxFolder = new File(Environment.getExternalStorageDirectory(), "EOLogger");
    return new File(gpxFolder.getPath(), "Sender.log");
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getFilePath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if ("com.android.externalStorage.documents".equalsIgnoreCase(uri.getAuthority())) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("primary".equalsIgnoreCase(type) || "content".equalsIgnoreCase(uri.getScheme())) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*  w  w  w.ja v  a2  s  .c  om*/
        }
        // DownloadsProvider
        else if ("com.android.providers.downloads.documents".equalsIgnoreCase(uri.getAuthority())) {
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));
            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if ("com.android.providers.media.documents".equalsIgnoreCase(uri.getAuthority())) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return "";
}

From source file:Main.java

/**
 * Get the external app cache directory.
 *
 * @param context The context to use/*  w w  w .  j  a va2s .  c om*/
 *
 * @return The external cache dir
 */
public static File getExternalCacheDir(Context context) {

    if (hasExternalCacheDir()) {
        return context.getExternalCacheDir();
    }

    // Before Froyo we need to construct the external cache dir ourselves
    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}

From source file:Main.java

public static String getExternalCacheDir(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        File f1 = context.getExternalCacheDir();
        if (f1 != null) {
            return f1.getPath();
        } else {//  w w w .  ja va 2s .  co  m
            return null;
        }
    } else {
        final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
        File f2 = Environment.getExternalStorageDirectory();
        if (f2 != null) {
            return f2.getPath() + cacheDir;
        } else {
            return null;
        }
    }
}

From source file:Main.java

private static File getExternalCacheDir() {
    File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
    File appCacheDir = new File(new File(dataDir, mContext.getPackageName()), "cache");
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
            return null;
        }/*from   w  ww . j  a va2  s .  c  o  m*/
    }
    return appCacheDir;
}

From source file:Main.java

/**
 * Checks whether the filename looks legitimate
 *//* w ww. ja va  2 s  .  c om*/
static boolean isFilenameValid(String filename, File downloadsDataDir) {
    final String[] whitelist;
    try {
        filename = new File(filename).getCanonicalPath();
        whitelist = new String[] { downloadsDataDir.getCanonicalPath(),
                Environment.getDownloadCacheDirectory().getCanonicalPath(),
                Environment.getExternalStorageDirectory().getCanonicalPath(), };
    } catch (IOException e) {
        Log.w(TAG, "Failed to resolve canonical path: " + e);
        return false;
    }

    for (String test : whitelist) {
        if (filename.startsWith(test)) {
            return true;
        }
    }

    return false;
}