How To Handle "android.os.FileUriExposedException" In Android

How To Handle "android.os.FileUriExposedException" In Android
 

Introduction

 
In this article, we will learn how to handle “android.os.FileUriExposedException” if we have an app that shares files with other apps using a URI on API 24+. As of Android N, we need to use FileProvider API.
 
Creating a New Project with Android Studio
  1. Open Android Studio and select "Create new project".
  2. Name the project as per your wish and select your activity template.
     
    How To Handle "android.os.FileUriExposedException" In Android
     
  3. Click the “Finish” button to create the new project in Android Studio.

Steps to change File Provider API

 
To replace “file://” to “uri://”, we should follow these three steps.
 
Step 1 - Change Manifest Entry
  1. Add the <provider /> tag with FileProvider inside the <application /> tag, as shown in the below code.
    1. <provider  
    2.     android:name="android.support.v4.content.FileProvider"  
    3.     android:authorities="${applicationId}.provider"  
    4.     android:exported="false"  
    5.     android:grantUriPermissions="true">  
    6.     <meta-data  
    7.         android:name="android.support.FILE_PROVIDER_PATHS"  
    8.         android:resource="@xml/provider_paths"/>  
    9. </provider>  
  1. Here, provider_paths is an XML file which is used to specify the path to be accessed via File Provider API for Android N and above devices.
  2. Don't forget to add the following permission in your manifest file.
    1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
Step 2 - Create an XML file in res/xml/provider_paths.xml
  1. Create a new XML folder in res. Then, create an XML file and name it as provider_paths.xml.
  2. Add the following code to the file. 
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <paths>  
    3.     <external-path name="external_files" path="."/>  
    4. </paths>  
Step 3 - Create an intent to open file for Android N
  1. Change the normal URI method for Android N.
    1. File file = new File("File Path");  
    2. Uri.fromFile(file)  
    To
    1. File file = new File("File Path");  
    2. Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);  
  1. Then, grant the Read URI permission for Android N & above devices. The following code shows how to use the "Open file" Intent for Android N devices and before Android N devices.
    1. File file = new File("File Path");  
    2. Intent intent = new Intent(Intent.ACTION_VIEW);  
    3. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);  
    4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
    5.     Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);  
    6.     intent.setDataAndType(apkURI, "image/jpg");  
    7.     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
    8. else {  
    9.     intent.setDataAndType(Uri.fromFile(file), "image/jpg");  
    10. }  
    11. startActivity(intent);  
Reference
 
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
If you have any doubts or need any help, contact me.
 
Next Article
 
It is the first part for accessing file URI for Android N & above devices. In the next article, we will learn how to access the Camera API for Android N and above devices.


Similar Articles