Search code examples
androidandroid-fileproviderstorage-access-framework

Saving file using Storage Access Framework in Android


I am integrating the Document Access Framework with my cloud storage access app. Using this documentation I have been able to access the file and retrieve it (I simply use Gmail app's attach function to check this).

I am now trying to find how to save file using the same method (Save file directly via the app to cloud storage) and I have done the following changes:

For the getRoots call,

row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE);

and I have also overriden the createDocument method.

I don't see much in ways of sample code or documentation on how to do this. I also see that a bunch of apps like the "Photos" app have "Share" button that use a different method (The logs show miniShareActivity) and my app does not show up in that (It looks like it is using a different filesharing mechanism)

I am looking for information on

  1. How to use the SAF to store file (any sample file will be great or pointers to documentation). I Am assuming it will allow the user to use the picker interface to navigate to folder and store the file.

  2. How to get the app to show up in the "Minishare activity" list of apps to import the file into the app (It looks like it does not provide a picker interface but I still would like to provide the support so the file is saved to a default location)


Solution

  • The Share button you're referring to in other apps probably uses ACTION_SEND as the Intent action, rather than ACTION_CREATE_DOCUMENT that was introduced with the Storage Access Framework.

    You probably want to support both, since ACTION_CREATE_DOCUMENT was only introduced in KitKat and older apps may not be aware of it. Similarly, you may want to also support Intent action ACTION_GET_CONTENT for read access on pre-KitKat devices (ACTION_GET_CONTENT is handled by the SAF when running on KitKat+, but your app still needs to handle it when running on a pre-KitKat device). Are the <intent-filter>s in your AndroidManifest set up to match all of those Intent types?