Introduction: In this article, we will learn How to handle “android.os.FileUriExposedException”, if we have an app that shares files w...

How to handle “android.os.FileUriExposedException” in Android How to handle “android.os.FileUriExposedException” in Android

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 an Uri on API 24+. As of Android N, we need to use FileProvider API 

Creating New Project with Android Studio

  1. Open Android Studio and Select Create new project. 
  2. Name the project as your wish and select your activity template.


  3. Click “finish” button to create new project in Android Studio.

Steps to Change File Provider API 

To replace “file://” to “uri://”, We should follow the three steps.

Step 1: Change Manifest Entry
  1. Add <provider /> tag with FileProvider inside the tag as shown in the below code.
    <provider
     android:name="android.support.v4.content.FileProvider"
     android:authorities="${applicationId}.provider"
     android:exported="false"
     android:grantUriPermissions="true">
     <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths"/>
    </provider>
  2. Here, provider_paths is a xml file which is used to specify the path to be accessed via File Provider API for Android N & above devices.
  3. Don't forget to add the following permission in your manifest file
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Step 2: Create XML file in res/xml/provider_paths.xml
  1. Create a new xml folder in res folder. Then create xml file and named as provider_paths.xml
  2. Add the following code to the file.
    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path name="external_files" path="."/>
    </paths>
Step 3: Create an Intent to open file for Android N
  1. Change the normal Uri method for Android N.
    File file = new File("File Path");
    Uri.fromFile(file)
    To
    File file = new File("File Path");
    Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);
  2. Then Grant Read Uri Permission for Android N & above devices. The following code shows how to use Open file Intent for Android N devices & before Android N devices.
    File file = new File("File Path");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
     Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);
     intent.setDataAndType(apkURI, "image/jpg");
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
     intent.setDataAndType(Uri.fromFile(file), "image/jpg");
    }
    startActivity(intent);

Reference:

https://developer.android.com/reference/android/support/v4/content/FileProvider.html
If you have any doubt or need any help, contact me.

Next Article:

It is a first part for accessing file uri for Android N & above devices. Next article, we will learn how to access Camera API for Android N & above devices.

1 comment:

  1. Serves24 is Best Air conditioner repair In Hyderabad We Provided Door Step Repair And Service & Provide best in class. Get 6 months repair guarantee. Book Now just @249/-. at most affordable prices
    air conditioner repair services
    air conditioner repairs services hyderabad
    air conditioner repair hyderabad
    air conditioner repair services

    Really Very Good Article

    ReplyDelete

Please Comment about the Posts and Blog