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 File getExternalCacheDir(Context context, String dirName) {
    File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Charismatic_yichang"), "data");
    File appCacheDir2 = new File(new File(dataDir, context.getPackageName()), "cache");
    File appCacheDir = new File(appCacheDir2, dirName);
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
            Log.w(TAG, "Unable to create external cache directory");
            return null;
        }/*from w  ww  .  j  a v a  2  s.  co m*/
        try {
            new File(appCacheDir, ".nomedia").createNewFile();
        } catch (IOException e) {
            Log.i(TAG, "Can't create \".nomedia\" file in application external cache directory");
        }
    }
    return appCacheDir;
}

From source file:Main.java

/**
 * @return Total external memory//from  w  w w . java  2s . c  om
 */
public static int getTotalExternalMemorySize() {
    if (isExternalStorageAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        int blockSize = stat.getBlockSize();
        int totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    } else {
        return 0;
    }
}

From source file:Main.java

public static String getOdkFolder() {
    String path = Environment.getExternalStorageDirectory() + File.separator + ODK_FOLDER_NAME;
    return path;
}

From source file:Main.java

/**
 * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
 * is mounted. Else - Android defines cache directory on device's file system.
 * //from www.  j  ava2  s  .c  o  m
 * @param context Application context
 * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
 * @return Cache {@link File directory}
 */
public static File getOwnCacheDirectory(Context context, String cacheDir) {
    File appCacheDir = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
    }
    if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
        appCacheDir = context.getCacheDir();
    }
    return appCacheDir;
}

From source file:Main.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders./*from  w  w w  .  j a v a2  s.c  om*/
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
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];
            }

            // 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:Main.java

private static String getAbsolutePathOnExternalStorage(final ApplicationInfo pApplicationInfo,
        final String pPath) {
    return Environment.getExternalStorageDirectory() + "/Android/data/" + pApplicationInfo.packageName
            + "/files/" + pPath;
}

From source file:Main.java

public static File getSDPath(Context context) {
    File sdDir = context.getCacheDir();
    boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        sdDir = Environment.getExternalStorageDirectory();
    }/*from  w w  w.j a  v a 2s  . c  o  m*/
    return sdDir;
}

From source file:Main.java

private static File getExternalCacheDir(Context context) {
    File path = null;/*from   ww  w  .  j  ava2 s.c  om*/
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        path = context.getExternalCacheDir();
        // In some case, even the sd card is mounted,
        // getExternalCacheDir will return null
        // may be it is nearly full.
    }
    if (null == path) {
        // Before Froyo or the path is null,
        // we need to construct the external cache folder ourselves
        final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
        path = new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
    }
    return path;
}

From source file:Main.java

public static boolean isInstalledOnSdCard(Context context) {
    // check for API level 8 and higher
    if (isCompatible(8)) {
        PackageManager pm = context.getPackageManager();
        try {//w  ww. j a  va 2  s  .c o  m
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            ApplicationInfo ai = pi.applicationInfo;
            return (ai.flags & 0x00040000 /*
                                           * ApplicationInfo.
                                           * FLAG_EXTERNAL_STORAGE
                                           */) == 0x00040000 /*
                                                              * ApplicationInfo.
                                                              * FLAG_EXTERNAL_STORAGE
                                                              */;
        } catch (NameNotFoundException e) {
            // ignore
        }
    }

    // check for API level 7 - check files dir
    try {
        String filesDir = context.getFilesDir().getAbsolutePath();
        if (filesDir.startsWith("/data/")) {
            return false;
        } else if (filesDir.contains(Environment.getExternalStorageDirectory().getPath())) {
            return true;
        }
    } catch (Throwable e) {
        // ignore
    }

    return false;
}