我在Android 5+中使用Storage Access Framework时遇到问题,我无法获得使用ACTION_OPEN_DOCUMENT或ACTION_CREATE_DOCUMENT打开的照片的写入权限。
Intent i=new Intent(Intent.ACTION_OPEN_DOCUMENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, 10);
//created objects like this in onActivityResult
Uri uri=data.getData();
DocumentFile df=DocumentFile.fromSingleUri(this, uri);
ParcelFileDescriptor pfdr=null, pfdw=null;
//below code in try catch block
pfdr=getContentResolver().openFileDescriptor(uri, "r"); //null means no read
pfdw=getContentResolver().openFileDescriptor(uri, "w"); //null means no write
为了验证是否有其他方法可行,我创建了一个小应用程序,我在其中使用ACTION_OPEN_DOCUMENT或ACTION_CREATE_DOCUMENT选择了一个图像,使用Cursor获取其文件名并创建以下对象:
我做错了什么,我还有其他办法获得在Android 5 +中打开照片的写入权限。
我还想提一件事,对于某些文件,我确实获得了写访问权,但我不明白它何时以及如何工作。
我也在清单中包含了MANAGE_DOCUMENTS权限。