Android 外部存储与 SD 卡 [英] Android External Storage vs. SD Card
问题描述
阅读有关存储文件的 Android 文档后,我发现外部存储可以包括可移动 SD 卡和设备内部存储,即不可移动.选择将文件保存到外部存储时,有没有办法区分可移动存储和不可移动存储?
After reading the Android documentation on storing files, I see that External Storage can include both a removable sd card AND storage that is internal to the device, i.e. not removable. Is there a way to distinguish between removable storage and non-removable storage when choosing to save a file to External Storage?
推荐答案
我认为您无法可靠地区分内部和外部 (SD) 存储.
I think you can't reliably distinguish between internal and external (SD) storage.
乍一看,您似乎可以使用 Environment.isExternalStorageRemovable()
之类的东西,但这并不可靠,因为您的主要外部"存储设备很可能是设备的内部存储器,而不是 SD 卡.
At first glance it might seem like you can use something like Environment.isExternalStorageRemovable()
but this isn't reliable, because your "primary external" storage device might very well be the device's internal memory, not the SD card.
Environment.getExternalStorageDirectory()
状态:
注意:不要被这里的外部"这个词混淆.这个目录最好被认为是媒体/共享存储.它是一个文件系统,可以保存相对大量的数据,并在所有应用程序之间共享(不强制执行权限).传统上,这是一张 SD 卡,但它也可以作为设备中的内置存储实现,与受保护的内部存储不同,可以作为文件系统安装在计算机上.
举个例子,我的 LG G4 安装了外置 SD 卡,我可以用 adb (external_SD
) 看到它:
As an example, my LG G4 has an external SD card installed, and I can see it with adb (external_SD
):
$ adb shell ls -la /storage/
drwx------ root root 2015-02-28 01:10 USBstorage1
drwx------ root root 2015-02-28 01:10 USBstorage2
drwx------ root root 2015-02-28 01:10 USBstorage3
drwx------ root root 2015-02-28 01:10 USBstorage4
drwx------ root root 2015-02-28 01:10 USBstorage5
drwx------ root root 2015-02-28 01:10 USBstorage6
dr-xr-xr-x root root 2015-02-28 01:10 emulated
drwxrwx--x root sdcard_r 2015-07-31 08:19 external_SD
lrwxrwxrwx root root 2015-02-28 01:10 sdcard0 -> /storage/emulated/legacy
但是,用于外部存储的各种 API 返回的值证明设备使用内部存储器作为其主要外部"存储:
However, the various APIs for external storage return values that prove that the device is using internal memory as its "primary external" storage:
- Environment.getExternalStorageState:已安装
- Environment.isExternalStorageEmulated:true
- Environment.isExternalStorageRemovable:false
- Context.getExternalCacheDir:/storage/emulated/0/Android/data/com.codeblast.storagetype/cache
- Context.getExternalFilesDir:/storage/emulated/0/Android/data/com.codeblast.storagetype/files
- Environment.getExternalStorageState: mounted
- Environment.isExternalStorageEmulated: true
- Environment.isExternalStorageRemovable: false
- Context.getExternalCacheDir: /storage/emulated/0/Android/data/com.codeblast.storagetype/cache
- Context.getExternalFilesDir: /storage/emulated/0/Android/data/com.codeblast.storagetype/files
在没有外部 SD 卡的模拟器上运行代码返回完全相同的结果.
Running the code on an emulator without external SD card returns exactly the same results.
因此您不能假设 /mnt/sdcard
表示物理 SD 卡.
So you can't assume that /mnt/sdcard
means a physical SD card.
您可能已经重新考虑了您实际尝试完成的任务,而不是尝试检测存储类型.:-)
You may have reconsider what you're actually trying accomplish rather than trying to detect the storage type. :-)
这篇关于Android 外部存储与 SD 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!