android - 如何在android中获取内部和外部sdcard路径

标签 android file android-sdcard android-file

大多数新的 android 设备都有一个内部 sdcard 和一个外部 sdcard。我想制作一个文件浏览器应用程序,但我不知道如何获取在我的应用程序中使用的路径,因为

File file = Environment.getExternalStorageDirectory();

只在大多数设备中返回 /mnt/sdcard 但其他外部 sdcard 有另一条路径,例如 /storage1 或/storage2 .任何帮助表示赞赏。

最佳答案

How to get the internal and external sdcard path in android

在内部存储中存储的方法:

File getDir (String name, int mode)

File getFilesDir () 

上述方法存在于Context

手机内存存储方法:

File getExternalStorageDirectory ()

File getExternalFilesDir (String type)

File getExternalStoragePublicDirectory (String type)

一开始大家都是用Environment.getExternalStorageDirectory(),指向手机内存的root。结果,根目录被随机内容填充。

后来增加了这两个方法:

Context 类中,他们添加了 getExternalFilesDir(),指向手机内存中的特定应用目录。卸载应用时,该目录及其内容将被删除。

Environment.getExternalStoragePublicDirectory() 用于存储知名文件类型(如照片和电影)的集中位置。此目录及其内容将在应用卸载时不会被删除。

存储在可移动存储(即 micro SD 卡)中的方法

在 API 级别 19 之前,没有官方的方式存储在 SD 卡中。但是许多人可以使用非官方的 API 来做到这一点。

官方在 API 级别 19(Android 版本 4.4 - Kitkat)的 Context 类中引入了一种方法。

File[] getExternalFilesDirs (String type)

It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

这意味着,它将返回到 Micro SD 卡和内部存储器的路径。一般第二个返回路径为micro SD卡的存储路径。

根据 Google/官方 Android 文档的内部和外部存储术语与我们的想法完全不同

关于android - 如何在android中获取内部和外部sdcard路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13361024/

相关文章:

android - 如何在模拟器中连接到本地主机

python - python 关联文件中的项目

python - 类型错误 : coercing to Unicode when run thru Windows CMD

android - 为什么我得到的手机和 SD 卡内存信息不正确?

android - 在 Android 中以编程方式卸载 SD 卡?

Android 检索项目 : No resource found that matches the given name 的父项时出错

android - 将图像共享到其他应用程序

android - 使用parse NoClassDefFoundError推送android通知:com.parse.Parse

file - 保存 Windows XP 和 Vista 应用程序用户信息的最佳位置

java - 强制访问外部可移动 microSD 卡