使用SAF获取持久性SD Cad写权限 [英] Get Persistable SD Cad write Permission using SAF

查看:162
本文介绍了使用SAF获取持久性SD Cad写权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,图像文件列表在recyclerview中被夸大.然后在应用程序编辑图像文件的元数据之后.

In my application, list of Image files is inflated in recyclerview. Then after application edits the metadata of Image file.

我用于编写元数据的代码:-

My code for writing metadata:-

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卡图像,却出现错误---->

I am able to update image file stored in phone memory, but for the Sd Card Images, I get error ---->

java.io.FileNotFoundException: Permission denied

我知道从android 5开始,我们需要使用SAF进行许可. 我的许可代码:-

I know that from android 5 , we need to take permission using SAF. My code for taking permission:-

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许可.

Now I dont know what to do with this treeUri. I want that I take SdCard Permission Once at initial startup only.

简单来说,我的问题与以下问题有关:-

此链接

推荐答案

DocumentFile documentFile = DocumentFile.fromTreeUri(this, treeUri);

在这里,treeUri是您从SAF许可中获得的.表示您在OnActivityResult()中得到的结果.

here treeUri is what you get from SAF permission. Means what you get in OnActivityResult().

DocumentFile fos = DocumentFile.fromFile(new File(fileIn));

DocumentFile fos = DocumentFile.fromFile(new File(fileIn));

在此,fileIn是您选择的文件.然后

In this, fileIn is your selected file. then

String[] parts = (fileIn.getPath()).split("\\/");
for (int i = 3; i < parts.length; i++) {
if (documentFile != null) {
    documentFile = documentFile.findFile(parts[i]);
}  
}

此后要获取byteArray,您需要使用此代码

hereafter for getting byteArray you need to use this code

try {
                        InputStream iStream = getContentResolver().openInputStream(documentFile.getUri());
                        byte[] byteArrayInputStream = getBytes(iStream);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

获取字节数组后,您想继续执行代码,并在下面提及.

After getting byte array you want to continue your code, and also i metion below.

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();
                    }

否:我认为这会对您有所帮助.如果发现任何错误,请更正.谢谢

Not: I think this will help you. If you find any mistakes please correct it. Thanks

这篇关于使用SAF获取持久性SD Cad写权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆