0

我只是尝试使用存储访问框架删除文件,但是 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)
4

1 回答 1

0

问题是您使用了 Uri.parse(t) 而 SAF 有自己的文档寻址系统。除非进行一些自定义映射,否则您无法将文件路径映射到 SAF 树。

这是 Xamarin 中的代码:

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
            if (requestCode == SRC_URI_REQUEST_CODE && resultCode == Result.Ok)
            {
                var androidUri = data.Data;
                var storageUri = DocumentsContract.BuildDocumentUriUsingTree(androidUri, DocumentsContract.GetTreeDocumentId(androidUri));
            }
    }

现在您需要遍历 storageUri 并使用 DocumentsContract.BuildChildDocumentsUriUsingTree 和 ContentResolver.Query 获取子 URI。

于 2016-10-13T22:54:10.997 回答