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:-
Saving the Uri with
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.
here treeUri is what you get from SAF permission. Means what you get in OnActivityResult().
In this, fileIn is your selected file. then
hereafter for getting byteArray you need to use this code
After getting byte array you want to continue your code, and also i metion below.