无法写入具有权限的SD卡

时间:2018-08-21 14:39:46

标签: java android android-sdcard

我一直在努力地在SD卡上写东西,最后我终于意识到,这项工作有一种新的“权限”。 首先,这是我使用的代码,希望获得许可,以后不再需要它:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, RCODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RCODE && resultCode == Activity.RESULT_OK){
        Uri treeUri = data.getData();
        DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
        grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        App.editor.putBoolean("writable",true).apply();App.editor.commit();
    }
}

这是我用来删除文件的代码:

public static boolean deleteFile(Context context, File f){
    if (isOnInternal(f)){
        return f.delete();
    }else{
        String path = "/document/";
        String tp = f.getAbsolutePath().replace(getsdpath(),"").substring(1);
        path += getsdpath().replace("/storage/","")+":"+tp;
        Uri uri = new Uri.Builder()
                .scheme("content")
                .authority("com.android.externalstorage.documents")
                .path(path)
                .build();
        Log.e("path1",uri.getPath());
        return DocumentFile.fromSingleUri(context,uri).delete();
    }
}

这是我要删除文件时的LogCat消息:

W/Documents: Failed to delete document
java.lang.SecurityException: Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/document/1CE2-0E0E%3Aimg.jpg from pid=3000, uid=10060 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1599)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
    at android.content.ContentProviderClient.call(ContentProviderClient.java:437)
    at android.provider.DocumentsContract.deleteDocument(DocumentsContract.java:1015)
    at android.provider.DocumentsContract.deleteDocument(DocumentsContract.java:999)
    at android.support.v4.provider.SingleDocumentFile.delete(SingleDocumentFile.java:98)
    at ir.coders.nahan.Utils.FileUtils.deleteFile(FileUtils.java:41)
    at ir.coders.nahan.App.deleteFile(App.java:314)
    at ir.coders.nahan.Activities.HidePics.lambda$onActivityResult$7(HidePics.java:230)
    at ir.coders.nahan.Activities.-$$Lambda$HidePics$Vwwkfk8tS-KV0NE8pJgYHtkKXnI.run(lambda)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

以下都是通过以下方法采用的路径:

uri.getPath();
the tree which I grant the permission from: /tree/1CE2-0E0E:
the file : /document/1CE2-0E0E:img.jpg

我用来删除文件的代码是尝试在用户选择文件时复制android itslef的原始URI,但它似乎并未按计划工作。

我将不胜感激!

2 个答案:

答案 0 :(得分:0)

通过清单或代码将MANAGE_DOCUMENTS添加到您的权限中。 您的其他权限似乎并不涵盖此特定权限。

答案 1 :(得分:0)

  

我从/ tree / 1CE2-0E0E授予许可的树:

     

文件:/document/1CE2-0E0E:img.jpg

URI的根不同。检查这两个URI,因为它们看起来不一样。我怀疑是问题所在。

您应该以某种方式重新使用发送给my_roundup <- function(x) { x <- as.numeric(format(x, digits = 2)) stopifnot(x >= 1.0 & x <= 5.0) floor(x) + (round(x %% 1,1) > 0.3)*0.5 + (round(x %% 1,1) > 0.7)*0.5 } 的URI