Skip to content Skip to sidebar Skip to footer

Get Persistable Sd Cad Write Permission Using Saf

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 = Docum

Solution 1:

DocumentFiledocumentFile= DocumentFile.fromTreeUri(this, treeUri);

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

DocumentFile fos = DocumentFile.fromFile(new File(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]);
}  
}

hereafter for getting byteArray you need to use this code

try {
                        InputStreamiStream= 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 {
                        ParcelFileDescriptorpfd= context.getContentResolver().openFileDescriptor(fos.getUri(), "w");
                        FileOutputStreamfileOutputStream=newFileOutputStream(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

Solution 2:

Saving the Uri with

takePersistableUriPermission()

is absolutely right to gain access. The permission is kept until uninstalling or clearing app data. Next step is to keep the Uri for later access (i.e. SharedPreferences).

This will help you.

Post a Comment for "Get Persistable Sd Cad Write Permission Using Saf"