FATAL EXCEPTION: main Process: com.sec.android.app.camera, PID: 12841 android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.example.test/cache/pickImageResult.jpeg exposed beyond app through Intent.getData() at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978) at android.net.Uri.checkFileUriExposed(Uri.java:2371) at android.content.Intent.prepareToLeaveProcess(Intent.java:10972) at android.content.Intent.prepareToLeaveProcess(Intent.java:10923) at android.app.Activity.finish(Activity.java:5718) at android.app.Activity.finish(Activity.java:5745) at com.sec.android.app.camera.AttachActivity.onOkay(AttachActivity.java:350) at com.sec.android.app.camera.AttachActivity.onClick(AttachActivity.java:107) at android.view.View.performClick(View.java:7341) at android.widget.TextView.performClick(TextView.java:14162) at android.view.View.performClickInternal(View.java:7307) at android.view.View.access$3200(View.java:846) at android.view.View$PerformClick.run(View.java:27796) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7156) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
2 Answers
Extend Application class and inside onCreate()
method put below code,
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
if (Build.VERSION.SDK_INT > 23) {
builder.detectFileUriExposure();
}
In Manifest file,
<application
android:name=".YOUR_CLASS_THAT_EXTEND_APPLICATION_CLASS"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Other stuffs -->
</application>

- 2,161
- 3
- 22
- 42
If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps. We create our own class inheriting FileProvider in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies as described here.
Steps to replace file:// URI with content:// URI:
Add a class extending FileProvider
public class GenericFileProvider extends FileProvider {}
Add a FileProvider tag in AndroidManifest.xml under tag. Specify a unique authority for the android:authorities attribute to avoid conflicts, imported dependencies might specify ${applicationId}.provider and other commonly used authorities.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
create a provider_paths.xml file in res/xml folder. Folder may be needed to created if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Uri photoURI = Uri.fromFile(createImageFile());
to
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", createImageFile());
Edit: If you're using an intent to make the system open your file, you may need to add the following line of code:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

- 3,830
- 1
- 15
- 29

- 2,395
- 4
- 20
- 49