为什么getExternalFilesDirs()在某些设备上不起作用? [英] Why getExternalFilesDirs() doesn't work on some devices?

查看:112
本文介绍了为什么getExternalFilesDirs()在某些设备上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在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返回sdcard的路径,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆