本文共 1249 字,大约阅读时间需要 4 分钟。
Android 9.0 系统对外置 SD 卡的读写权限进行了更严格的管理,导致许多应用无法直接通过 Java.io.File 类操作外置 SD 卡。以下是针对该问题的详细分析及解决方案。
在 Android 9.0 及以上版本中,外置 SD 卡的读写权限需要通过 DocumentFile 接口来操作。系统提供了 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 两个权限,但这些权限的动态申请在某些情况下仍然无法对外置 SD 卡创建文件夹。
DocumentFile documentFile = DocumentFile.fromFile(new File("/storage/sdcard1/test.txt"));String rootPath = getStoragePath(this, true);
OutputStream outputStream = documentFile.getOutputStream(new File("/storage/sdcard1/test.txt"));对于系统定制开发,直接修改系统文件可以避免对所有应用进行兼容性修改。以下是系统文件中相关权限的修改方法:
allow system_app:apk_data_file:file {create write unlink setattr};makeadb push SC200Y/target/product/msm8953_64/system/etc/sepolcy /system/etc/adb reboot
adb shell ps -A | grep sdcard
String rootPath = getStoragePath(this, true);
DocumentFile documentFile = DocumentFile.fromFile(new File(rootPath + "/test"));
boolean canWrite = canWrite(context, new File(rootPath + "/test"));
通过以上方法,可以在 Android 9.0 及以上版本中实现对外置 SD 卡的读写权限管理。
转载地址:http://xxqn.baihongyu.com/