无法写入具有权限的 SD 卡
Can't write to SD Card with permissions
我已经在 sd 卡上写了很长一段时间了,我终于意识到有一种新的 "permissions" 专门用于这项工作。
首先,这是我使用的代码,希望获得许可,以后不需要:
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(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,但它似乎没有按计划工作。
如有任何帮助,我将不胜感激!
在清单中或通过代码将 MANAGE_DOCUMENTS
添加到您的权限中。
您的其他权限似乎没有涵盖此特定权限。
the tree which I grant the permission from: /tree/1CE2-0E0E:
the file : /document/1CE2-0E0E:img.jpg
URI 的根不同。检查这两个 URI,因为它们看起来并不相似。我怀疑这是问题所在。
您应该以某种方式重新使用您发送的 URI grantUriPermission()
我已经在 sd 卡上写了很长一段时间了,我终于意识到有一种新的 "permissions" 专门用于这项工作。 首先,这是我使用的代码,希望获得许可,以后不需要:
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(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,但它似乎没有按计划工作。
如有任何帮助,我将不胜感激!
在清单中或通过代码将 MANAGE_DOCUMENTS
添加到您的权限中。
您的其他权限似乎没有涵盖此特定权限。
the tree which I grant the permission from: /tree/1CE2-0E0E:
the file : /document/1CE2-0E0E:img.jpg
URI 的根不同。检查这两个 URI,因为它们看起来并不相似。我怀疑这是问题所在。
您应该以某种方式重新使用您发送的 URI grantUriPermission()