1

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()

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
Sathish
  • 73
  • 1
  • 6
  • 1
    that isn't crop problem. There were changes in file access system in Android 7. You have to implement your content provider from now on to enable other apps to use your provided Uris – Vladyslav Matviienko May 19 '17 at 06:54
  • `exposed beyond app through ClipData.Item.getUri()` I wonder how that would be possible. – greenapps May 19 '17 at 06:58
  • 1
    https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – CommonsWare May 19 '17 at 10:43

1 Answers1

7

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

Dinesh
  • 482
  • 9
  • 20
  • 3
    Hi @Dinesh, the above code is working for above nougat? I got the popup like **Editing is not supported for this image** ,Is there any mistake from myside..? – VV W Jun 25 '18 at 06:20