Android N FileUriExposedException

Posted: December 27, 2016 in Android, Technology
Tags: , , , , , , ,

Hello droids!

Here is another quick solution for not letting your app get crash in Nougat devices.

Mostly crash occurs with file permission while taking photo or selecting image from the gallery in Nougat devices.

In general you will get FileUriExposedException.

How to solve this issue?
Here is the quick solution, you need to follow 3 – 4 steps and voila you’re done!

Step 1 : provider_paths.xml
Create provider_paths.xml to /res/xml directory

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

Step 2 : AndroidManifst file update
Open your manifest file and add below tags in between <application> </application> tags.

<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>

 

Step 3 : Application class edit
Hope you are aware of how to declare application class. If not do comment on this post, I’ll let you know.

@Override
public void onCreate() {
    super.onCreate();
    //Allowing Strict mode policy for Nougat support
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
}

Now, you are ready to access URI files from nougat enabled devices.
Don’t forget to add appropriate permissions to your manifest file for camera and external storage.

Have fun! Keep coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s