为什么 getExternalFilesDirs() 在某些设备上不起作用? [英] Why getExternalFilesDirs() doesn't work on some devices?
问题描述
我的应用在 Android 5.0 上运行.我使用方法 getExternalFilesDirs()
来检查外部 SD 卡是否可用.如果返回超过 1 个 File
,则表示存在外部 SD 卡.
My app runs on Android 5.0. I use method getExternalFilesDirs()
to check if external SD card is available. If it returns more than 1 File
, that means external SD card exists.
但在某些设备上(例如 Elephone G2),方法 getExternalFilesDirs()
仅返回主存储的一个目录.我确定该设备具有外部 SD 卡 (/storage/sdcard1/).
But on some devices (for example Elephone G2), method getExternalFilesDirs()
returns only one directory of primary storage. I'm sure that device has external SD card (/storage/sdcard1/).
谁能给我答案?
推荐答案
为了让 getExternalFilesDirs 返回 SD 卡的路径,OEM 必须在设备特定的 init.rc 中设置 SECONDARY_STORAGE
环境变量此处提到的文件:https://source.android.com/devices/storage/config-example.html
For getExternalFilesDirs to return the path of the sdcard, the OEM must have set the SECONDARY_STORAGE
environment variable in the device specific init.rc file as mentioned here:
https://source.android.com/devices/storage/config-example.html
在此处查看 getExternalFilesDirs
的来源:http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/app/ContextImpl.java#1039
Look at the source of getExternalFilesDirs
here:
http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/app/ContextImpl.java#1039
该值是从 Environment.buildExternalStorageAppFilesDirs
获得的.看看这里的来源:http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#206
The value is obtained from Environment.buildExternalStorageAppFilesDirs
. Look at that source here:
http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#206
该值取决于 mExternalDirsForApp
,后者又通过读取 SECONDARY_STORAGE 变量的内容来填充:http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#136
The value is dependent on mExternalDirsForApp
, which in turn is populated by reading the contents of SECONDARY_STORAGE variable:
http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#136
如您所见,如果未设置 SECONDARY_STORAGE 变量,则不会返回 sdcard 路径.您可以通过转到 adb shell
并查看 echo $SECONDARY_STORAGE
As you can see, if the SECONDARY_STORAGE variable is not set, the sdcard path will not be returned.
You can cross-check this by going to adb shell
and looking at the output of echo $SECONDARY_STORAGE
这篇关于为什么 getExternalFilesDirs() 在某些设备上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!