我正在尝试使用存储访问框架从外部 SD 卡中删除文件。我通过拥有 SD 卡根 Uri 然后使用 DocumentFile 的不同方法访问文件层次结构中的特定文档来获得访问权限。这就是我的做法
我正在使用以下代码访问外部 SD 卡根目录 -
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
MainActivity.hub.startActivityForResult(intent, TREE_ACCESS_REQUEST_CODE);
然后我保存 treeUri 并请求持久许可
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NoobDeleteConfirmManager.TREE_ACCESS_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri treeUri = null;
if (data != null) {
// Get Uri from Storage Access Framework.
treeUri = data.getData();
NoobFileUtils.setTreeUri(treeUri);
}
}
}
//In NoobFileUtils.setTreeUri
public static void setTreeUri(Uri treeUriParam) {
if (treeUriParam == null)
return;
mTreeUri = treeUriParam;
// Persist URI in shared preference so that you can use it later.
// Use your own framework here instead of PreferenceUtil.
new PrefUtils().setRootSDCardUriLollipop(mTreeUri);
// Persist access permissions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
MainActivity.hub.getContentResolver().takePersistableUriPermission(mTreeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
然后我得到要删除的文件的 absolutePath 并尝试获取它的 DocumentFile 对象。
File file = new File(absolutePath);
DocumentFile _documentFile = DocumentFile.fromFile(file);
此绝对路径深入到外部 SD 卡的层次结构中。然而,这给了我一个分段错误,根本没有例外。我也尝试使用以下代码从文件中获取 DocumentFile -
document = DocumentFile.fromTreeUri(MainActivity.hub, mTreeUri);
这给了我同样的问题。(MainActivity.hub
是 MainActivity 的单例。不要怪我,我只是在这里修复错误)。
不应该是这样的吗?仅使用 Root Uri 权限访问驻留在 SD 卡内的文件有什么问题。不是为什么ACTION_OPEN_DOCUMENT_TREE
被创建而不是ACTION_OPEN_DOCUMENT
. 无论如何,我在 Marshmallow 中使用它,所以它应该可以工作。
这是我得到的分段错误的崩溃日志。
--------- beginning of crash
09-06 23:25:50.070 20947-20957/com.app A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xc in tid 20957 (HeapTaskDaemon)
09-06 23:25:50.083 20947-20953/com.app A/art: art/runtime/debugger.cc:514] Check failed: c != nullptr
09-06 23:25:50.177 356-356/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
09-06 23:25:50.177 356-356/? A/DEBUG: Build fingerprint: 'asus/WW_Phone/ASUS_Z010_CD:6.0.1/MMB29P/13.8.26.36-20160624:user/release-keys'
09-06 23:25:50.177 356-356/? A/DEBUG: Revision: '0'
09-06 23:25:50.177 356-356/? A/DEBUG: ABI: 'arm64'
09-06 23:25:50.177 356-356/? A/DEBUG: pid: 20947, tid: 20957, name: HeapTaskDaemon >>> com.app <<<
09-06 23:25:50.177 356-356/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xc
[ 09-06 23:25:50.189 18966:18966 E/ ]
[adb] handle_packet() t->online(1) p->msg.arg0(412) p->msg.arg1(0) OPEN
[ 09-06 23:25:50.189 18966:18966 E/ ]
[adb] handle the adb command, and the command = adb shell:cat /proc/20947/stat
09-06 23:25:50.197 356-356/? A/DEBUG: x0 0000007f9e03b000 x1 0000007f9e03f2e0 x2 0000000070efa980 x3 0000155555545400
09-06 23:25:50.198 356-356/? A/DEBUG: x4 000000000000002a x5 0000000004fe8000 x6 0000007fa14767c8 x7 0000020000000000
09-06 23:25:50.198 356-356/? A/DEBUG: x8 00000055ad5652b0 x9 0000000000040000 x10 0000000012ca2300 x11 0000000012ca2380
09-06 23:25:50.198 356-356/? A/DEBUG: x12 00000000000004dd x13 00000000000004dd x14 0000000012cb3610 x15 0000000000080001
09-06 23:25:50.198 356-356/? A/DEBUG: x16 00000055ad5652b0 x17 0000007f9dc2b000 x18 00000055ad561320 x19 0000000000000016
09-06 23:25:50.198 356-356/? A/DEBUG: x20 0005550000440000 x21 0000000012ca30b0 x22 0000000012ca3000 x23 0000007f8c3c4b90
09-06 23:25:50.198 356-356/? A/DEBUG: x24 0000000000000000 x25 0000007f8c3c4b98 x26 0000000000000001 x27 0000000000400000
09-06 23:25:50.198 356-356/? A/DEBUG: x28 0000000012c00000 x29 0000007f8c3c4ab0 x30 0000007f9dc2d1a4
09-06 23:25:50.198 356-356/? A/DEBUG: sp 0000007f8c3c4ab0 pc 0000007f9dc2cc68 pstate 0000000080000000
[ 09-06 23:25:50.204 18966:18966 E/ ]
[adb] cuurent command is A_CLSE
09-06 23:25:50.206 356-356/? A/DEBUG: backtrace:
09-06 23:25:50.207 356-356/? A/DEBUG: #00 pc 000000000021ec68 /system/lib64/libart.so (_ZN3art6mirror6Object15VisitReferencesILb0ELNS_17VerifyObjectFlagsE0ENS_2gc9collector17MarkObjectVisitorENS5_29DelayReferenceReferentVisitorEEEvRKT1_RKT2_+64)
09-06 23:25:50.207 356-356/? A/DEBUG: #01 pc 000000000021f1a0 /system/lib64/libart.so (_ZNK3art2gc10accounting11SpaceBitmapILm8EE16VisitMarkedRangeINS0_9collector17ScanObjectVisitorEEEvmmRKT_+448)
09-06 23:25:50.207 356-356/? A/DEBUG: #02 pc 000000000021fbdc /system/lib64/libart.so (_ZN3art2gc9collector9MarkSweep15ScanGrayObjectsEbh+2600)
09-06 23:25:50.207 356-356/? A/DEBUG: #03 pc 0000000000220128 /system/lib64/libart.so (_ZN3art2gc9collector9MarkSweep25RecursiveMarkDirtyObjectsEbh+24)
09-06 23:25:50.207 356-356/? A/DEBUG: #04 pc 00000000002206d8 /system/lib64/libart.so (_ZN3art2gc9collector9MarkSweep12MarkingPhaseEv+344)
09-06 23:25:50.207 356-356/? A/DEBUG: #05 pc 00000000002209d8 /system/lib64/libart.so (_ZN3art2gc9collector9MarkSweep9RunPhasesEv+732)
09-06 23:25:50.207 356-356/? A/DEBUG: #06 pc 0000000000212b48 /system/lib64/libart.so (_ZN3art2gc9collector16GarbageCollector3RunENS0_7GcCauseEb+296)
09-06 23:25:50.207 356-356/? A/DEBUG: #07 pc 0000000000243e1c /system/lib64/libart.so (_ZN3art2gc4Heap22CollectGarbageInternalENS0_9collector6GcTypeENS0_7GcCauseEb+2056)
09-06 23:25:50.207 356-356/? A/DEBUG: #08 pc 0000000000245688 /system/lib64/libart.so (_ZN3art2gc4Heap16ConcurrentGCTask3RunEPNS_6ThreadE+152)
09-06 23:25:50.207 356-356/? A/DEBUG: #09 pc 0000000000268f3c /system/lib64/libart.so (_ZN3art2gc13TaskProcessor11RunAllTasksEPNS_6ThreadE+80)
09-06 23:25:50.207 356-356/? A/DEBUG: #10 pc 0000000001f3b56c /system/framework/arm64/boot.oat (offset 0x1f3b000)
[ 09-06 23:25:50.226 18966:18966 E/ ]
[adb] handle_packet() t->online(1) p->msg.arg0(413) p->msg.arg1(0) OPEN
[ 09-06 23:25:50.226 18966:18966 E/ ]
[adb] handle the adb command, and the command = adb shell:cat /proc/stat
[ 09-06 23:25:50.240 18966:18966 E/ ]
[adb] cuurent command is A_CLSE
[ 09-06 23:25:50.390 18966:18966 E/ ]
[adb] handle_packet() t->online(1) p->msg.arg0(414) p->msg.arg1(0) OPEN
[ 09-06 23:25:50.390 18966:18966 E/ ]
[adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10172
[ 09-06 23:25:50.422 18966:18966 E/ ]
[adb] cuurent command is A_CLSE
09-06 23:25:50.457 356-356/? W/debuggerd64: type=1400 audit(0.0:580): avc: denied { search } for name="com.app" dev="mmcblk0p44" ino=601485 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0
09-06 23:25:50.577 356-356/? W/debuggerd64: type=1400 audit(0.0:581): avc: denied { search } for name="com.app" dev="mmcblk0p44" ino=601485 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0
09-06 23:25:50.587 356-356/? W/debuggerd64: type=1400 audit(0.0:582): avc: denied { search } for name="com.app" dev="mmcblk0p44" ino=601485 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0
09-06 23:25:50.637 356-356/? W/debuggerd64: type=1400 audit(0.0:583): avc: denied { search } for name="com.app" dev="mmcblk0p44" ino=601485 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0
09-06 23:25:50.637 356-356/? W/debuggerd64: type=1400 audit(0.0:584): avc: denied { search } for name="com.app" dev="mmcblk0p44" ino=601485 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0
09-06 23:25:50.657 356-356/? W/debuggerd64: type=1400 audit(0.0:585): avc: denied { search } for name="com.app" dev="mmcblk0p44" ino=601485 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0
--------- beginning of system
09-06 23:25:50.723 1727-21902/? W/ActivityManager: Force finishing activity com.app/com.activity.MainActivity
09-06 23:25:50.724 356-356/? A/DEBUG: Tombstone written to: /data/tombstones/tombstone_03
09-06 23:25:50.724 356-356/? E/DEBUG: AM write failed: Broken pipe
09-06 23:25:50.725 1727-1897/? I/BootReceiver: Copying /data/tombstones/tombstone_03 to DropBox (SYSTEM_TOMBSTONE)