android.os.Environment.getExternalStorageDirectory()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(517)

本文整理了Java中android.os.Environment.getExternalStorageDirectory()方法的一些代码示例,展示了Environment.getExternalStorageDirectory()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Environment.getExternalStorageDirectory()方法的具体详情如下:
包路径:android.os.Environment
类名称:Environment
方法名:getExternalStorageDirectory

Environment.getExternalStorageDirectory介绍

暂无

代码示例

代码示例来源:origin: jeasonlzy/okhttp-OkGo

  1. public FileConvert(String fileName) {
  2. this(Environment.getExternalStorageDirectory() + DM_TARGET_FOLDER, fileName);
  3. }

代码示例来源:origin: Curzibn/Luban

  1. /**
  2. * <b>BuildTime:</b> 2014-10-22<br>
  3. * <b>Description:</b> get SDCard path<br>
  4. *
  5. * @return String of path
  6. */
  7. public static String getSDCardPath() {
  8. return Environment.getExternalStorageDirectory().getPath();
  9. }

代码示例来源:origin: Curzibn/Luban

  1. private String getPath() {
  2. String path = Environment.getExternalStorageDirectory() + "/Luban/image/";
  3. File file = new File(path);
  4. if (file.mkdirs()) {
  5. return path;
  6. }
  7. return path;
  8. }

代码示例来源:origin: facebook/stetho

  1. private static File resolvePossibleSdcardPath(String path) {
  2. if (path.startsWith("/")) {
  3. return new File(path);
  4. } else {
  5. return new File(Environment.getExternalStorageDirectory(), path);
  6. }
  7. }

代码示例来源:origin: CarGuo/GSYVideoPlayer

  1. private static File getExternalCacheDir(Context context) {
  2. File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
  3. File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
  4. if (!appCacheDir.exists()) {
  5. if (!appCacheDir.mkdirs()) {
  6. return null;
  7. }
  8. }
  9. return appCacheDir;
  10. }
  11. }

代码示例来源:origin: CarGuo/GSYVideoPlayer

  1. private static File getExternalCacheDir(Context context) {
  2. File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
  3. File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
  4. if (!appCacheDir.exists()) {
  5. if (!appCacheDir.mkdirs()) {
  6. HttpProxyCacheDebuger.printfWarning("Unable to create external cache directory");
  7. return null;
  8. }
  9. }
  10. return appCacheDir;
  11. }
  12. }

代码示例来源:origin: robolectric/robolectric

  1. @Implementation
  2. protected File getExternalCacheDir() {
  3. return Environment.getExternalStorageDirectory();
  4. }

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

  1. /**
  2. * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
  3. * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system.
  4. *
  5. * @param context Application context
  6. * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
  7. * @return Cache {@link File directory}
  8. */
  9. public static File getOwnCacheDirectory(Context context, String cacheDir) {
  10. File appCacheDir = null;
  11. if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
  12. appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
  13. }
  14. if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
  15. appCacheDir = context.getCacheDir();
  16. }
  17. return appCacheDir;
  18. }

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

  1. /**
  2. * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
  3. * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system.
  4. *
  5. * @param context Application context
  6. * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
  7. * @return Cache {@link File directory}
  8. */
  9. public static File getOwnCacheDirectory(Context context, String cacheDir, boolean preferExternal) {
  10. File appCacheDir = null;
  11. if (preferExternal && MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
  12. appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
  13. }
  14. if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
  15. appCacheDir = context.getCacheDir();
  16. }
  17. return appCacheDir;
  18. }

代码示例来源:origin: android-hacker/VirtualXposed

  1. public static File getVirtualStorageBaseDir() {
  2. File externalFilesRoot = Environment.getExternalStorageDirectory();
  3. if (externalFilesRoot != null) {
  4. File vBaseDir = new File(externalFilesRoot, "VirtualXposed");
  5. File vSdcard = new File(vBaseDir, "vsdcard");
  6. return ensureCreated(vSdcard);
  7. }
  8. return null;
  9. }

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

  1. private static File getExternalCacheDir(Context context) {
  2. File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
  3. File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
  4. if (!appCacheDir.exists()) {
  5. if (!appCacheDir.mkdirs()) {
  6. L.w("Unable to create external cache directory");
  7. return null;
  8. }
  9. try {
  10. new File(appCacheDir, ".nomedia").createNewFile();
  11. } catch (IOException e) {
  12. L.i("Can't create \".nomedia\" file in application external cache directory");
  13. }
  14. }
  15. return appCacheDir;
  16. }

代码示例来源:origin: TeamNewPipe/NewPipe

  1. public File getBackTop() {
  2. if (getArguments() == null) return Environment.getExternalStorageDirectory();
  3. final String path = getArguments().getString(KEY_START_PATH, "/");
  4. if (path.contains(Environment.getExternalStorageDirectory().getPath())) {
  5. return Environment.getExternalStorageDirectory();
  6. }
  7. return getPath(path);
  8. }

代码示例来源:origin: TeamNewPipe/NewPipe

  1. @Override
  2. protected AbstractFilePickerFragment<File> getFragment(@Nullable String startPath, int mode, boolean allowMultiple, boolean allowCreateDir, boolean allowExistingFile, boolean singleClick) {
  3. final CustomFilePickerFragment fragment = new CustomFilePickerFragment();
  4. fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
  5. mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
  6. return currentFragment = fragment;
  7. }

代码示例来源:origin: android-hacker/VirtualXposed

  1. public static File getVirtualPrivateStorageDir(int userId) {
  2. String base = String.format(Locale.ENGLISH, "%s/Android/data/%s/%s/%d", Environment.getExternalStorageDirectory(),
  3. VirtualCore.get().getHostPkg(), "virtual", userId);
  4. File file = new File(base);
  5. return ensureCreated(file);
  6. }

代码示例来源:origin: greenrobot/greenDAO

  1. private File getBenchFile(String name) {
  2. File dir = Environment.getExternalStorageDirectory();
  3. File file = new File(dir, name);
  4. if (dir == null || !dir.canWrite()) {
  5. Application app = createApplication(Application.class);
  6. File appFile = new File(app.getFilesDir(), name);
  7. DaoLog.d("Using file " + appFile.getAbsolutePath() + ", (cannot write to " + file.getAbsolutePath() + ")");
  8. file = appFile;
  9. }
  10. return file;
  11. }

代码示例来源:origin: Justson/AgentWeb

  1. public static long getAvailableStorage() {
  2. try {
  3. StatFs stat = new StatFs(Environment.getExternalStorageDirectory().toString());
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  5. return stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
  6. } else {
  7. return (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
  8. }
  9. } catch (RuntimeException ex) {
  10. return 0;
  11. }
  12. }

代码示例来源:origin: robolectric/robolectric

  1. @Test
  2. public void getExternalStorageDirectory_shouldReturnDirectory() {
  3. assertThat(Environment.getExternalStorageDirectory().exists()).isTrue();
  4. }

代码示例来源:origin: jeasonlzy/okhttp-OkGo

  1. private OkDownload() {
  2. folder = Environment.getExternalStorageDirectory() + File.separator + "download" + File.separator;
  3. IOUtils.createFolder(folder);
  4. threadPool = new DownloadThreadPool();
  5. taskMap = new ConcurrentHashMap<>();
  6. //校验数据的有效性,防止下载过程中退出,第二次进入的时候,由于状态没有更新导致的状态错误
  7. List<Progress> taskList = DownloadManager.getInstance().getDownloading();
  8. for (Progress info : taskList) {
  9. if (info.status == Progress.WAITING || info.status == Progress.LOADING || info.status == Progress.PAUSE) {
  10. info.status = Progress.NONE;
  11. }
  12. }
  13. DownloadManager.getInstance().replace(taskList);
  14. }

代码示例来源:origin: robolectric/robolectric

  1. @Test
  2. @Config(minSdk = LOLLIPOP)
  3. public void storageIsLazy() {
  4. assertNull(ShadowEnvironment.EXTERNAL_CACHE_DIR);
  5. assertNull(ShadowEnvironment.EXTERNAL_FILES_DIR);
  6. Environment.getExternalStorageDirectory();
  7. Environment.getExternalStoragePublicDirectory(null);
  8. assertNotNull(ShadowEnvironment.EXTERNAL_CACHE_DIR);
  9. assertNotNull(ShadowEnvironment.EXTERNAL_FILES_DIR);
  10. }

代码示例来源:origin: robolectric/robolectric

  1. @Test
  2. @Config(minSdk = M)
  3. public void isExternalStorageRemovable_primaryShouldReturnSavedValue() {
  4. assertThat(Environment.isExternalStorageRemovable()).isFalse();
  5. ShadowEnvironment.setExternalStorageRemovable(Environment.getExternalStorageDirectory(), true);
  6. assertThat(Environment.isExternalStorageRemovable()).isTrue();
  7. }

相关文章