How to Share Files Securely with FileProvider


1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)


Tung Dao Xuan,, is the author of this article and he contributes to RobustTechHouse Blog



FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri (Uniform Resource Identifier) for a file instead of a file:/// Uri. To find out why we should use this class to share the files managed by your application with other applications, you can check it here.

In this tutorial, we are going to create an application to demonstrate how to use FileProvider for file sharing.


Creating an application

This application has one button each to save a file to internal storage, cache folder and external storage. The three buttons shown in the diagram below corresponds to the above mentioned features to demonstrate sharing files in three locations. Please take a look at following diagram for more clarity.


Saving a file to multiple locations

Defining a FileProvider

Since the default functionality of FileProvider includes content URI generation for files, you don’t need to define a subclass in the codes. Instead, you can include a FileProvider in your app by specifying it entirely in XML in the AndroidManifest.xml file.


  • Set the android:authorities attribute to a URI authority based on a domain you control; for example, if you control the domain com you should use the authority com.mydomain.fileprovider.
  • Set the android:resource to a xml file that specifies the available files. This file will be created in next section.


Specifying Available Files

Create file_paths.xml file in folder res/xml in the project that contains following content:


Represents files in the files/ subdirectory of your app’s internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir().


Represents files in the root of your app’s external storage area. The path Context.getExternalFilesDir() returns the files/ subdirectory of this root.


Represents files in the cache subdirectory of your app’s internal storage area. The root path of this subdirectory is the same as the value returned by getCacheDir()


The attributes:


A URI path segment. This value hides the name of the subdirectory you’re sharing. The subdirectory name for this value is contained in the path attribute.


The subdirectory you’re sharing.


Sharing a file



This variable AUTHORITIES_NAME must be equal to name that is specified in the android:authorities attribute in the provider above.

Intent.FLAG_GRANT_READ_URI_PERMISSION the recipient of this Intent will be granted permission to perform read operations on the URI in the Intent’s data.


Source code


Brought to you by RobustTechHouse. We provide E-Commerce Development services.

Recommended Posts
Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
Neural NetworkDeep Learning Video