Android : How to write camera intent in android nougat

Hey please follow this thread as a reference. It will show you how to use File Provider when you set your targetSDK as 24 and change following. In your private static Uri getImageFileUri() method

Change this line

return Uri.fromFile(image);

to

FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());

Hope this will help you to solve your issue.

For more go to –
Setting Up File Sharing – Offical documentation

Try this its not the intent that create the problem once you take the picture and save to the sd card and getting back the uri is different in Nougat….

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: AndroidManifest.xml

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

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.

res/xml/provider_paths.xml

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

The final step is to change the line of code below in MainActivity.java

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

to

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. Cheers !

Well it is Android’s job to make developers life a living hell with each update 🙂

googlers, here is a step by step guide for developers who (like the question) have used the samples in Android documentations;

1- in the part you have used

Uri.fromFile(image)

you need to use this snippet:

Uri photoURI = FileProvider.getUriForFile(mContext,
                        "com.sample.test.fileprovider",
                        image);

of course it is needless to say that you have to change com.sample.test to your package name.

2- now you need to declare your provider in your AndroidManifest.xml to do so, under Application tag, paste this tag:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.sample.test.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />

3- pay attention to android:resource="@xml/file_paths" you need to create an xml file with same name file_paths under your res/xml/ folder and put this in it:

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

on couple of other snippets on the web, and the documentation itself, it says you need to write this

<external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />

instead of ours, it is actually depend on your code, if you create your File using Environment.getExternalStorageDirectory().getPath() you do not need it, but if you followed exactly like the docs, you need to stick to the docs