Sathish
Sathish

Reputation: 83

Android camera crop not supporting in Nougat 7

Camera and Gallery Crop is not supported in android nougat 7 while opening camera in android nougat 7 I am getting this error message.

android.os.FileUriExposedException:

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

Upvotes: 1

Views: 3097

Answers (1)

Dinesh
Dinesh

Reputation: 482

For apps targeting Android 7.0, the Android framework enforces the StrictMode API policy that prohibits exposing file:// URIs outside your app. If an intent containing a file URI leaves your app, the app fails with a FileUriExposedException exception.

To share files between applications, you should send a content:// URI and grant a temporary access permission on the URI. The easiest way to grant this permission is by using the FileProvider class.

You can Try My Solution..

1.add res/xml/provider_paths.xml

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

2.add in Manifest inside tag

     <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="android3.maxtingapp.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

3.in your activity add funtion for Crop Image like below my sample

private void cropImage(File file) {
       final int width  = 400;
       final int height = 200;

   try {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");

    Uri contentUri;

        if(Build.VERSION.SDK_INT > M){

             contentUri = FileProvider.getUriForFile(AddPlace.this,
                                   "android3.maxtingapp.provider",
                                    file);//package.provider

            //TODO:  Permission.. 

            getApplicationContext().grantUriPermission("com.android.camera",
                                                         contentUri,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        }else{

            contentUri = Uri.fromFile(file);

        }

        cropIntent.setDataAndType(contentUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 2);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", width);
        cropIntent.putExtra("outputY", height);

        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, REQUEST_CROP_ICON);

    }catch (ActivityNotFoundException a) {
        Log.e("Activity Not Found",""+a.toString());
    }
}

I Hope this would be helpfull for someOne..

Upvotes: 7

Related Questions