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();}
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
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);
}
}
}
Now I dont know what to do with this treeUri. I want that I take SdCard Permission Once at initial startup only.
In simple terms my question relates to this question:-
This Link