Android Capture Image Camera Gallery Using FileProvider With Examples

In this tutorial, we’ll be developing an application which displays images captured from camera or gallery using FileProvider. We’ve already developed a similar application in the past. But with the introduction of Android Nougat, it gives a runtime crash saying FileUriExposedException.


FileProvider is a special subclass of ContentProvider which allows sharing of files between application through content URI instead of file:// URI.

Using file:// URI is not the best idea. It gives all apps the permission to access the files once the Storage Permissions are granted.

We somehow need to restrict this such that the user knows the applications with which it would be sharing the files.

For this, we use FileProviders which allow temporary access permissions to the files. Otherwise, we were able to access files from other apps by simply getting their URI from Uri.parse()

By using FileProvider in your app, you do not need to ask user to grant WRITE_EXTERNAL_STORAGE permission everytime.

Defining FileProvider

To define a FileProvider in our android application, we need to do the following things:

  • Define the FileProvider in your AndroidManifest file
  • Create an XML file that contains all paths that the FileProvider will share with other applications

Create an xml folder inside the res directory.
Add the provider_paths.xml file in it:

Depending on the storage we need to access, we pass the value in the external-path.
Example of other values that can be passed – sdcard

Now let’s write our Version 2.0 Application of Capturing Image from Camera And Gallery that works on Android Nougat and above.

Project Structure


The AndroidManifest.xml with all the permissions looks like:



The code for the activity_main.xml layout is:

The code for the is given below:

Following is are the methods which get the content uri from the filepath provided that the FileProvider has granted the appropriate permissions:

Android Capture Image Camera Gallery Using FileProvider With Examples

The output of the above application in action is given below:

android fileprovider camera gallery output

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply