我只是尝试使用存储访问框架删除文件,但是 deleteDocument 总是返回 false。我有一个装有 Android 5.0 的 Asus zenfone 5 我只是想知道 contentprovider 和 uri 是否正确。uri 是来自 SDCard(/storage/MicroSD/test123123.apk) 的路径
String t = "";
void deleteFunc(String filepath) {
t = filepath;
Intent intent = new Intent("android.intent.action.OPEN_DOCUMENT_TREE");
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "S: " + data.getData(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "D: " + t, Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
DocumentFile test = DocumentFile.fromFile(new File(t));
DocumentsContract.deleteDocument(getApplicationContext().getContentResolver(), Uri.parse(t));
Toast.makeText(this, "P: " + test.delete(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "T: " + DocumentsContract.deleteDocument(getApplicationContext().getContentResolver(), Uri.parse(t)), Toast.LENGTH_SHORT).show();
}
}
这是 deleteDocument 的例外
09-23 11:46:13.054 8216 8216 W Documents: Failed to delete document
09-23 11:46:13.054 8216 8216 W Documents: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.ContentProviderClient.call(java.lang.String, java.lang.String, android.os.Bundle)' on a null object reference
09-23 11:46:13.054 8216 8216 W Documents: at android.provider.DocumentsContract.deleteDocument(DocumentsContract.java:987)
09-23 11:46:13.054 8216 8216 W Documents: at android.provider.DocumentsContract.deleteDocument(DocumentsContract.java:971)
09-23 11:46:13.054 8216 8216 W Documents: at com.test.example.ResultActivity.onActivityResult(SourceFile:364)
09-23 11:46:13.054 8216 8216 W Documents: at android.app.Activity.dispatchActivityResult(Activity.java:6177)
09-23 11:46:13.054 8216 8216 W Documents: at android.app.ActivityThread.deliverResults(ActivityThread.java:3551)
09-23 11:46:13.054 8216 8216 W Documents: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3598)
09-23 11:46:13.054 8216 8216 W Documents: at android.app.ActivityThread.access$1300(ActivityThread.java:147)
09-23 11:46:13.054 8216 8216 W Documents: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
09-23 11:46:13.054 8216 8216 W Documents: at android.os.Handler.dispatchMessage(Handler.java:102)
09-23 11:46:13.054 8216 8216 W Documents: at android.os.Looper.loop(Looper.java:135)
09-23 11:46:13.054 8216 8216 W Documents: at android.app.ActivityThread.main(ActivityThread.java:5253)
09-23 11:46:13.054 8216 8216 W Documents: at java.lang.reflect.Method.invoke(Native Method)
09-23 11:46:13.054 8216 8216 W Documents: at java.lang.reflect.Method.invoke(Method.java:372)
09-23 11:46:13.054 8216 8216 W Documents: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
09-23 11:46:13.054 8216 8216 W Documents: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)