且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 getExternalFilesDirs() 在某些设备上不起作用?

更新时间:2023-01-15 21:12:58

为了让 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