FileUriExposedException using Android 7

When I try to capture an image, I have this error:

FATAL EXCEPTION: main android.os.FileUriExposedException: file: ///storage/emulated/0/fname_1498727381241.jpg is exposed outside the app via ClipData.Item.getUri () on android.os.StrictMode.onFileUriExposed (StrictMode.java:1799 ) at android.net.Uri.checkFileUriExposed (Uri.java:2346) at android.content.ClipData.prepareToLeaveProcess (ClipData.java:835) at android.content.Intent.prepareToLeaveProcess (Intent.java:9514) at android.content.Intent.prepareToLeaveProcess (Intent.java:9499) at android.app.Instrumentation.execStartActivity (Instrumentation.java:1525) at android.app.Activity.startActivityForResult (Activity.java:4403) at android. app.Activity.startActivityForResult (Activity.java:4362) at opteamit.com.belami.CommuniquerPartagerPhotosActivity $ 1.onClick (CommuniquerPartagerPhotosActivity.java:46) at android.view.View.performClick (View.java: 6261) on android.view.View $ PerformClick.run (View.java:23752) on android.os.Handler.handleCallback (Handler.java:751) on android.os.Handler.dispatchMessage (Handler.java:95 ) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6776) at java.lang.reflect.Method.invoke (native method) at com.android. internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)

It worked before, but the problem seems to be that I am using Android 7 (API 24).

This is my code:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fname_" +
                        String.valueOf(System.currentTimeMillis()) + ".jpg"));
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

      

+3


source to share


2 answers


If your target SdkVersion is 24 or higher, we have to use the FileProvider class to provide access to a specific file or folder in order to make it available to other applications. We create our own class that inherits FileProvider to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies, as described here.

Find a suitable discussion here



Since Android 7 we don't use file schema as uri for intent, you have to use FileProvider.

+6


source


Well, the correct way to do it is to use FileProvider

( as stated by the developer ). But I figured out how to do it without adding FileProvider

. This is obviously a workaround, but it works.

In your activity add the following lines:

 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
 StrictMode.setVmPolicy(builder.build());

      



This will just ignore the exposure of the URI and you will get access.

Yes, I know this is not the best practice. But I just wanted to provide him with an alternative.

However, it is recommended to use FileProvider

.

+1


source







All Articles