Search This Blog

Sunday, September 25, 2016

file:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24 (Android Nougat). What todo?

Android Nougat is almost be publicly released. And as an Android developer, we need to prepare ourself to adjust targetSdkVersion to the latest one, 24, to let everything works perfectly on the newest release of Android.

And as always, everytime we adjust targetSdkVersion, we need to check and make sure that every single part of our code works perfectly fine. If you just simply change the number, I could say that your application is taking a high risk of crashing or malfunction. In this case, when you change your app's targetSdkVersion to 24, we need to check that every single function works flawlessly on Android Nougat (24).

And this is one of the checklist you need to mark done before releasing your new version. There is one big security change on Android N like quoted below:

Passing file:// URIs outside the package domain may leave the receiver with an unaccessible path. Therefore, attempts to pass a file:// URI trigger a FileUriExposedException. The recommended way to share the content of a private file is using the FileProvider

Summarily, file:// is not allowed to attach with Intent anymore or it will throw FileUriExposedException which may cause your app crash immediately called.

This blog will talk about this issue and also about the solution how to make it work on Android N.

Real example with a crashing problem

You may be curious which situation that can really cause the problem. So to make it be easy to you all, let me show you a real usage example that causes crashing. The easiest example is the way we take a photo through Intent with ACTION_IMAGE_CAPTURE type. Previously we just pass the target file path with file:// format as an Intent extra which works fine on Android Pre-N but will just simply crash on Android N and above.

If you are running the code with targetsdkversion set to 23. But this will give you an following exception when you set targetsdkversion to 24.

FATAL EXCEPTION: main Process: com.inthecheesefactory.lab.intent_fileprovider, PID: 28905 
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/Camera/IMG_25092016.jpg exposed beyond app through ClipData.Item.getUri() 
at android.os.StrictMode.onFileUriExposed( 
at android.content.ClipData.prepareToLeaveProcess( ...

The reason is quite obvious. file:// is not allowed as an attached URI in Intent or FileUriExposedException would be thrown.

And this is a big issue that you have to make sure that all code related to this case has already been fixed before releasing a new version with targetSdkVersion 24 or your app may crash on some of your user's device.

Why nougat does not allow passing file:// with intent any more?

You may be curious why Android team decide to change this behavior. Actually there is a good reason behind.

If file path is sent to the target application (Camera app in this case), file will be fully accessed through the Camera app's process not the sender one.

But let's consider thoroughly, actually Camera is launched by our application to take a photo and save as a file on our app's behalf. So the access right to that file should be our app's not Camera's. Every operation did with the file should be done through our application not by Camera app itself.

And that's why file:// is now prohibited on targetSdkVersion 24 to force every developer to do this task in the proper way.

Solution is as below :

So if file:// is not allowed anymore, which approach should we go for? The answer is we should send the URI through content:// scheme instead which is the URI scheme for Content Provider. In this case, we would like to share an access to a file through our app so FileProvider is needed to be implemented. Flow is now changed like below:

And now, with FileProvider, file operation would be done through our app process like it supposes to be !

It is quite easy to implement FileProvider on your application. First you need to add a FileProvider<provider> tag in AndroidManifest.xml under <application> tag like below:


<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android=""


                               <meta-data android:name=""                                                                 android:resource="@xml/provider_paths"/>

And then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist.

The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.


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

FileProvider is now declared and be ready to use.

The last step is to change the line of code below in Activity

Uri photoURI = Uri.fromFile(createImageFile());


Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
               BuildConfig.APPLICATION_ID +
               ".provider", createImageFile());

You are done, Your code should run in all the previous version with nougat.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.