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

The following are Jave code examples for showing how to use isExternalStorageEmulated() of the android.os.Environment class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: BookyMcBookface   File: FsTools.java   View Source Code Vote up 6 votes
public static Map<File,String> getDrives() {
    File r = new File("/storage");


    Map<File,String> files = new LinkedHashMap<>();

    int sd = 1;
    for (File e: r.listFiles()) {
        Log.d("storage", e.getPath() + " " + e.isDirectory());
        if (e.isDirectory() && !e.getName().equals("emulated") && !e.getName().equals("self")) {
            String name = "SD";
            if (sd++>1) name += sd;
            files.put(e, Environment.isExternalStorageRemovable(e) ? name : e.getName());
        }
    }

    File ext = Environment.getExternalStorageDirectory();
    if (Environment.isExternalStorageEmulated()) {
        files.put(ext, "Internal");
    } else if (Environment.isExternalStorageRemovable()) {
        files.put(ext, "SD");
    }

    return files;
}
 
Example 2
Project: Quran   File: StorageUtils.java   View Source Code Vote up 5 votes
/**
 * 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
 */
private static List<Storage> buildMountsList(Context context, 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 3
Project: IslamicLibraryAndroid   File: StorageUtils.java   View Source Code Vote up 5 votes
/**
 * 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
 */
private static List<Storage> buildMountsList(Context context, 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 4
Project: AvenueNet   File: NetClientCreator.java   View Source Code Vote up 5 votes
private static OkHttpClient.Builder addInterceptor() {
    if (INTERCEPTORS != null) {
        for (Interceptor interceptor : INTERCEPTORS) {
            if (!(isApiTest && interceptor instanceof ApiTestInterceptor)) {
                break;
            } else {
                BUILDER.addInterceptor(interceptor);
            }
        }
    }

    final boolean isCache = AvenueNet.getConfig(ConfigKey.COMMON_CACHE);
    if (isCache) {
        final Context context = AvenueNet.getAppContext();
        final int size = 5 * 1024;
        final File cacheFile;
        if (Environment.isExternalStorageEmulated()) {
            cacheFile = context.getExternalCacheDir();
        } else {
            cacheFile = context.getCacheDir();
        }
        final Cache cache = new Cache(cacheFile, size);
        final CacheInterceptor cacheInterceptor = new CacheInterceptor();
        BUILDER.addNetworkInterceptor(cacheInterceptor);
        BUILDER.cache(cache);
        BUILDER.addInterceptor(cacheInterceptor);
    }
    return BUILDER;
}
 
Example 5
Project: MobileMedia   File: ImageLoader.java   View Source Code Vote up 5 votes
/**
 * 获取缓存的路径
 *
 * @param uniqueName
 * @return
 */
private File getDiskCacheDir(String uniqueName) {
    boolean isSDCardMounted = Environment.isExternalStorageEmulated();
    boolean isSDCardUnRemovable = Environment.isExternalStorageRemovable();
    String path;
    if (isSDCardMounted || isSDCardUnRemovable) {
        path = mContext.getExternalCacheDir().getPath();
    } else {
        path = mContext.getCacheDir().getPath();
    }
    File file = new File(path + File.separator + uniqueName);
    if (!file.exists())
        file.mkdirs();
    return file;
}
 
Example 6
Project: Quran   File: StorageUtils.java   View Source Code Vote up 4 votes
/**
 * @return A List of all storage locations available
 */
public static List<Storage> getAllStorageLocations(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) {
        result.add(new Storage(context.getString(typeId, number++),
            mountPoint.getAbsolutePath()));
        typeId = R.string.prefs_sdcard_external;
      }
    }
    return result;
  } else {
    return getLegacyStorageLocations(context);
  }
}
 
Example 7
Project: IslamicLibraryAndroid   File: StorageUtils.java   View Source Code Vote up 4 votes
/**
 * @return A List of all storage locations available
 */
public static List<Storage> getAllStorageLocations(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) {
                result.add(new Storage(context.getString(typeId, number++),
                        mountPoint.getAbsolutePath()));
                typeId = R.string.prefs_sdcard_external;
            }
        }
        return result;
    } else {
        return getLegacyStorageLocations(context);
    }
}
 
Example 8
Project: pre-dem-android   File: LogUtils.java   View Source Code Vote up 4 votes
/**
 * 判断SD卡是否mounted
 */
@SuppressLint("NewApi")
public static boolean isMounted() {
    return Environment.isExternalStorageEmulated();
}
 
Example 9
Project: FreeStreams-TVLauncher   File: LogUtil.java   View Source Code Vote up 4 votes
/** 判断SD卡是否mounted*/
public static boolean isMounted() {
    return Environment.isExternalStorageEmulated();
}
 
Example 10
Project: microMathematics   File: CompatUtils.java   View Source Code Vote up 4 votes
public static boolean isExternalStorageEmulated()
{
    return Environment.isExternalStorageEmulated();
}
 
Example 11
Project: SmartMath   File: AndroidStorageOptions.java   View Source Code Vote up 4 votes
@SuppressLint("NewApi")
@SuppressWarnings("unchecked")
   private static void setProperties() {
       /*
        * At this point all the paths in the list should be valid. Build the
        * public properties.
        */
       mLabels = new ArrayList<String>();

       int j = 0;
       if (mMounts.size() > 0) {
           if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD)
               mLabels.add(AppSmartMath.getContext().getString(R.string.auto));
           else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
               if (Environment.isExternalStorageRemovable()) {
                   mLabels.add(AppSmartMath.getContext().getString(R.string.external_sd_card) + " 1");
                   j = 1;
               } else
                   mLabels.add(AppSmartMath.getContext().getString(R.string.internal_storage));
           } else {
               if (!Environment.isExternalStorageRemovable()
                       || Environment.isExternalStorageEmulated())
                   mLabels.add(AppSmartMath.getContext().getString(R.string.internal_storage));
               else {
                   mLabels.add(AppSmartMath.getContext().getString(R.string.external_sd_card) + " 1");
                   j = 1;
               }
           }

           if (mMounts.size() > 1) {
               for (int i = 1; i < mMounts.size(); i++) {
                   mLabels.add(AppSmartMath.getContext().getString(R.string.external_sd_card)  + (i + j));
               }
           }
       }

       labels = new String[mLabels.size()];
       mLabels.toArray(labels);

       paths = new String[mMounts.size()];
       mMounts.toArray(paths);
       count = Math.min(labels.length, paths.length);

   }