首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SAF获得永久SD Cad写权限

使用SAF获得永久SD Cad写权限
EN

Stack Overflow用户
提问于 2016-08-17 13:38:13
回答 2查看 1.3K关注 0票数 0

在我的应用程序中,图像文件列表在回收视图中被放大。应用后对图像文件的元数据进行编辑。

我编写元数据的代码:-

代码语言:javascript
复制
DocumentFile fos = DocumentFile.fromFile(new File(fileIn));

ByteSource bytsInputStream = new ByteSourceFile(new File(fileIn));
byte[] byteArrayInputStream = bytsInputStream.getAll();

try {
    ParcelFileDescriptor pfd = context.getContentResolver().
            openFileDescriptor(fos.getUri(), "w");
    FileOutputStream fileOutputStream =
            new FileOutputStream(pfd.getFileDescriptor());

    rewriter.updateExifMetadataLossy(byteArrayInputStream,fileOutputStream,outputSet);

    fileOutputStream.close();
    pfd.close();

}
catch (Exception e){e.printStackTrace();}

我能够更新存储在手机内存中的图像文件,但是对于Sd卡图像,我会得到错误-->

代码语言:javascript
复制
java.io.FileNotFoundException: Permission denied

我知道从android 5,我们需要获得许可使用SAF。我的许可代码:-

代码语言:javascript
复制
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(
        Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE);












@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQUEST_CODE_OPEN_DOCUMENT_TREE:
            if (resultCode == Activity.RESULT_OK) {
                Uri treeUri = data.getData();
                int takeFlags = data.getFlags();
                takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
                              Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    this.getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
                }
           }
    }

现在我不知道如何处理这个treeUri。我希望我只在初始启动时获得SdCard许可一次。

,简单地说,我的问题与这个问题有关:-

This Link

EN

回答 2

Stack Overflow用户

发布于 2017-03-23 09:53:10

代码语言:javascript
复制
DocumentFile documentFile = DocumentFile.fromTreeUri(this, treeUri);

在这里,treeUri是从SAF许可中得到的。意思是在OnActivityResult()中得到了什么。

DocumentFile fos =DocumentFile.fromFile(新文件(FileIn));

在这里,fileIn是您选择的文件。然后

代码语言:javascript
复制
String[] parts = (fileIn.getPath()).split("\\/");
for (int i = 3; i < parts.length; i++) {
if (documentFile != null) {
    documentFile = documentFile.findFile(parts[i]);
}  
}

接下来,要获得byteArray,您需要使用以下代码

代码语言:javascript
复制
try {
                        InputStream iStream = getContentResolver().openInputStream(documentFile.getUri());
                        byte[] byteArrayInputStream = getBytes(iStream);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

在获得字节数组后,您希望继续您的代码,同时我在下面列出。

代码语言:javascript
复制
try {
                        ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(fos.getUri(), "w");
                        FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
                        rewriter.updateExifMetadataLossy(byteArrayInputStream,fileOutputStream,outputSet);
                        fileOutputStream.close();
                        pfd.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

不,我想这会对你有帮助。如果你发现任何错误,请改正。谢谢

票数 1
EN

Stack Overflow用户

发布于 2016-08-19 12:19:26

将Uri保存在

代码语言:javascript
复制
takePersistableUriPermission()

进入是绝对正确的。该权限一直保留到卸载或清除应用程序数据为止。下一步是保留Uri以供以后访问(即SharedPreferences)。

This会帮你的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38998276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档