androidgoogle-drive-apistorage-access-framework

Update File in GoogleDrive with Android Storage Access Framework


What I want: Copy a file from GoogleDrive folder to my apps folder, modify the file and after this copy back the file to the original place.

What I do at the moment: I use the Storage Access Framework (SAF) file picker to select the file in GoogleDrive. As result I got a DocumentFile which I can copy to the internal place of my app. I store the Uri of the file for later use

How to write back the file to the original place?

What I did

  1. I wanted to get an Outputstream and write the new data back using getContentResolver().openOutputstream(<original Uri>). In this case I get an security exception.
  2. I start an Intent.ACTION_OPEN_DOCUMENT and handle the selected result Uri, but here the user has to select the original file. It seems that I cannot give an initial path/name. The picker is always in the last used folder. Very unfriendly for the user
  3. I integrated the GoogleDrive Rest API, but here the app sees only the files, which were created by the app. Does this change when the app in the Google Cloud platform and in Google Developer Console is published?

I there another way to do this? Thx and br Klaus


Solution

  • When you get the URI returned from ACTION_OPEN_DOCUMENT, call context.getContentResolver().takePersistableUriPermission(uri,Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    Then later you will be able to use the URI to write the document using ContentResolver.openOutputStream(uri).