且构网

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

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

更新时间:2023-01-15 21:08:34

为使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