Nougat does not allow passing file:// with Intent anymore?

July 29, 2017 , 0 Comments

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 =======================
Solution :

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

STEP 1: It is quite easy to implement FileProvider on your application. First you need to add a FileProvider tag in AndroidManifest.xml under tag like below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<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>
</application>
</manifest>

STEP 2 : res/xml/provider_paths.xml

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="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

Done! FileProvider is now declared and be ready to use.

STEP 3 : How to Use it.

This is Used Previously .
Uri photoURI = Uri.fromFile(createImageFile());

Now Changed :

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

And .... done ! Your application should now work perfectly fine on any Android version including Android Nougat. Yah !

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts