0

I am trying to open a camera to take picture by using the code its work proper up to android 6.0 . But in android version 7.0 it is giving error

Error

file:///storage/emulated/0/04082017_1136image.jpg exposed beyond app through ClipData.Item.getUri()

private void takePhotoFromCamera()
{
    AnimateImageButton();
    boolean result = Utility.checkPermission(MainActivity.this);
    if (result) {
        try {
            _isOpenGallery = false;

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + "image.jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(intent, REQUEST_CAMERA);
            System.out.println("Hello >>>>>>>> : " + file.getAbsolutePath());
        }catch (Exception e)
        {

            Log.d("logforcamera",e.getMessage());
        }
    }
}

activity result code

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp + "image.jpg");
    System.out.println("Helllloooo >>>>>>>>>>> : " + file.getAbsolutePath());
    Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), widthX, heightY);
    System.out.println("Bitmap : " + bitmap);
    if (bitmap != null)
    {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        showSelectedImage(bitmap);
    }
Hamza
  • 251
  • 3
  • 14
  • put onActivityResult code – Deep Naik Aug 04 '17 at 06:53
  • it is giving error on. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); – Hamza Aug 04 '17 at 06:54
  • 1
    Possible duplicate of [android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) – SripadRaj Aug 04 '17 at 06:58
  • it is giving error only in android 7.0 – Hamza Aug 04 '17 at 06:59
  • in other android versions its working fine – Hamza Aug 04 '17 at 06:59
  • yes nougut is the version in which you have to give FileProvider to access camera image as a Uri in your App.just go through this link https://stackoverflow.com/questions/38555301/android-taking-picture-with-fileprovider – 9spl Aug 04 '17 at 07:05
  • the link you give. in that link the other problem is disscussed – Hamza Aug 04 '17 at 07:07
  • in that problem camera is opeing and saving the picture – Hamza Aug 04 '17 at 07:08
  • but in my case even camera is not opeing app is crashing – Hamza Aug 04 '17 at 07:08

2 Answers2

2

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

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

1

To open camera in targetSdkVersion 24 or higher version we have to use FileProvider class to access the files.

see this answer Open Camera in Nougut

Diwan Dhanvani
  • 291
  • 1
  • 3
  • 10