Java Code Examples for android.os.Environment#isExternalStorageEmulated()

The following examples show how to use android.os.Environment#isExternalStorageEmulated() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageOptionsBase.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
@SuppressLint("ObsoleteSdkInt")
private StorageInfo getDefaultStorage()
{
    String defPathState = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(defPathState) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(defPathState))
    {
        StorageInfo info = new StorageInfo();
        if (
                Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD ||
                        (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && !Environment.isExternalStorageRemovable()) ||
                        (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && (!Environment.isExternalStorageRemovable() || Environment.isExternalStorageEmulated()))
                )
            info.label = _context.getString(R.string.built_in_memory_card);
        else
        {
            info.isExternal = true;
            info.label = _context.getString(R.string.external_storage) + " 1";
        }
        info.path = Environment.getExternalStorageDirectory().getPath();
        return info;
    }
    return null;
}
 
Example 2
Source File: Paths.java    From YalpStore with GNU General Public License v2.0 6 votes vote down vote up
static public File getStorageRoot(Context context) {
    File storageRoot = Environment.getExternalStorageDirectory();
    File[] externalFilesDirs = getExternalFilesDirs(context);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
        || externalFilesDirs.length < 2
        || (Environment.isExternalStorageEmulated(storageRoot) && !Environment.isExternalStorageRemovable(storageRoot))
    ) {
        return storageRoot;
    }
    for (File file: externalFilesDirs) {
        try {
            if (null != file && Environment.isExternalStorageEmulated(file) && !Environment.isExternalStorageRemovable(file)) {
                return new File(file.getAbsolutePath().replace(FALLBACK_DIRECTORY, ""));
            }
        } catch (IllegalArgumentException e) {
            // The checks throw exceptions if Environment.getStorageVolume(File path) returns null
            // It would be great to know why
        }
    }
    return storageRoot;
}
 
Example 3
Source File: DiskStatsFileLogger.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void addAppsToJson(JSONObject json) throws JSONException {
    JSONArray names = new JSONArray();
    JSONArray appSizeList = new JSONArray();
    JSONArray appDataSizeList = new JSONArray();
    JSONArray cacheSizeList = new JSONArray();

    long appSizeSum = 0L;
    long appDataSizeSum = 0L;
    long cacheSizeSum = 0L;
    boolean isExternal = Environment.isExternalStorageEmulated();
    for (Map.Entry<String, PackageStats> entry : filterOnlyPrimaryUser().entrySet()) {
        PackageStats stat = entry.getValue();
        long appSize = stat.codeSize;
        long appDataSize = stat.dataSize;
        long cacheSize = stat.cacheSize;
        if (isExternal) {
            appSize += stat.externalCodeSize;
            appDataSize += stat.externalDataSize;
            cacheSize += stat.externalCacheSize;
        }
        appSizeSum += appSize;
        appDataSizeSum += appDataSize;
        cacheSizeSum += cacheSize;

        names.put(stat.packageName);
        appSizeList.put(appSize);
        appDataSizeList.put(appDataSize);
        cacheSizeList.put(cacheSize);
    }
    json.put(PACKAGE_NAMES_KEY, names);
    json.put(APP_SIZES_KEY, appSizeList);
    json.put(APP_CACHES_KEY, cacheSizeList);
    json.put(APP_DATA_KEY, appDataSizeList);
    json.put(APP_SIZE_AGG_KEY, appSizeSum);
    json.put(APP_CACHE_AGG_KEY, cacheSizeSum);
    json.put(APP_DATA_SIZE_AGG_KEY, appDataSizeSum);
}
 
Example 4
Source File: Uri.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * If this {@link Uri} is {@code file://}, then resolve and return its
 * canonical path. Also fixes legacy emulated storage paths so they are
 * usable across user boundaries. Should always be called from the app
 * process before sending elsewhere.
 *
 * @hide
 */
public Uri getCanonicalUri() {
    if ("file".equals(getScheme())) {
        final String canonicalPath;
        try {
            canonicalPath = new File(getPath()).getCanonicalPath();
        } catch (IOException e) {
            return this;
        }

        if (Environment.isExternalStorageEmulated()) {
            final String legacyPath = Environment.getLegacyExternalStorageDirectory()
                    .toString();

            // Splice in user-specific path when legacy path is found
            if (canonicalPath.startsWith(legacyPath)) {
                return Uri.fromFile(new File(
                        Environment.getExternalStorageDirectory().toString(),
                        canonicalPath.substring(legacyPath.length() + 1)));
            }
        }

        return Uri.fromFile(new File(canonicalPath));
    } else {
        return this;
    }
}
 
Example 5
Source File: StorageUtils.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts a list of mount strings to a list of Storage items
 *
 * @param context the context
 * @param mounts  a list of mount points as strings
 * @return a list of Storage items that can be rendered by the ui
 */
@NonNull
private static List<Storage> buildMountsList(@NonNull Context context, @NonNull List<String> mounts) {
    List<Storage> list = new ArrayList<>(mounts.size());

    int externalSdcardsCount = 0;
    if (mounts.size() > 0) {
        // Follow Android SD Cards naming conventions
        if (!Environment.isExternalStorageRemovable() || Environment.isExternalStorageEmulated()) {
            list.add(new Storage(context.getString(R.string.prefs_sdcard_internal),
                    Environment.getExternalStorageDirectory().getAbsolutePath()));
        } else {
            externalSdcardsCount = 1;
            list.add(new Storage(context.getString(R.string.prefs_sdcard_external,
                    externalSdcardsCount), mounts.get(0)));
        }

        // All other mounts rather than the first mount point are considered as External SD Card
        if (mounts.size() > 1) {
            externalSdcardsCount++;
            for (int i = 1/*skip the first item*/; i < mounts.size(); i++) {
                list.add(new Storage(context.getString(R.string.prefs_sdcard_external,
                        externalSdcardsCount++), mounts.get(i)));
            }
        }
    }

    Timber.d("final storage list is: %s", list);
    return list;
}
 
Example 6
Source File: DefaultAndroidEventProcessor.java    From sentry-android with MIT License 4 votes vote down vote up
private boolean isExternalStorageMounted() {
  final String storageState = Environment.getExternalStorageState();
  return (Environment.MEDIA_MOUNTED.equals(storageState)
          || Environment.MEDIA_MOUNTED_READ_ONLY.equals(storageState))
      && !Environment.isExternalStorageEmulated();
}
 
Example 7
Source File: StorageUtils.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return A List of all storage locations available
 */
@NonNull
public static List<Storage> getAllStorageLocations(@NonNull Context context) {

/*
  This first condition is the code moving forward, since the else case is a bunch
  of unsupported hacks.

  For Kitkat and above, we rely on Environment.getExternalFilesDirs to give us a list
  of application writable directories (none of which require WRITE_EXTERNAL_STORAGE on
  Kitkat and above).

  Previously, we only would show anything if there were at least 2 entries. For M,
  some changes were made, such that on M, we even show this if there is only one
  entry.

  Irrespective of whether we require 1 entry (M) or 2 (Kitkat and L), we add an
  additional entry explicitly for the sdcard itself, (the one requiring
  WRITE_EXTERNAL_STORAGE to write).

  Thus, on Kitkat, the user may either:
  a. not see any item (if there's only one entry returned by getExternalFilesDirs, we won't
  show any options since it's the same sdcard and we have the permission and the user can't
  revoke it pre-Kitkat), or
  b. see 3+ items - /sdcard, and then at least 2 external fiels directories.

  on M, the user will always see at least 2 items (the external files dir and the actual
  external storage directory), and potentially more (depending on how many items are returned
  by getExternalFilesDirs).
 */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        List<Storage> result = new ArrayList<>();
        int limit = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 1 : 2;
        final File[] mountPoints = ContextCompat.getExternalFilesDirs(context, null);
        if (mountPoints != null && mountPoints.length >= limit) {
            int typeId;
            if (!Environment.isExternalStorageRemovable() || Environment.isExternalStorageEmulated()) {
                typeId = R.string.prefs_sdcard_internal;
            } else {
                typeId = R.string.prefs_sdcard_external;
            }

            int number = 1;
            result.add(new Storage(context.getString(typeId, number),
                    Environment.getExternalStorageDirectory().getAbsolutePath(),
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.M));
            for (File mountPoint : mountPoints) {
                if (mountPoint != null) {
                    result.add(new Storage(context.getString(typeId, number++),
                            mountPoint.getAbsolutePath()));
                    typeId = R.string.prefs_sdcard_external;
                }
            }
        }
        return result;
    } else {
        return getLegacyStorageLocations(context);
    }
}
 
Example 8
Source File: LogUtils.java    From pre-dem-android with MIT License 4 votes vote down vote up
/**
 * 判断SD卡是否mounted
 */
@SuppressLint("NewApi")
public static boolean isMounted() {
    return Environment.isExternalStorageEmulated();
}
 
Example 9
Source File: CompatUtils.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isExternalStorageEmulated()
{
    return Environment.isExternalStorageEmulated();
}
 
Example 10
Source File: LogUtil.java    From android-tv-launcher with MIT License 4 votes vote down vote up
/** 判断SD卡是否mounted*/
public static boolean isMounted() {
    return Environment.isExternalStorageEmulated();
}
 
Example 11
Source File: Storage.java    From batteryhub with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if external storage is emulated, works on API level 11+.
 *
 * @return True if method is supported and storage is emulated
 */
private static boolean isExternalStorageEmulated() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB &&
            Environment.isExternalStorageEmulated();
}