If your android application shares files with/from other applications using Uri, and your application's targetSdkVersion is 24+ i.e Android N, then you may have encountered with an error/exception saying something like android.os.FileUriExposedException:
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/Camera/img_21032017190911.jpg exposed beyond app through ClipData.Item.getUri()
The error or exception is thrown when an application exposes a file:// Uri to another app.
Thus using file:// Uri is discouraged in this scenario because the receiving app may not have access to the path or the file:// Uri shared with it.
Like for example, the receiving app may not have requested the READ_EXTERNAL_STORAGE run time permission, or the platform may be sharing the file:// Uri across user profile boundaries, then this may result in an unexpected behaviour at best or at worst, it may result in a crash.
So the first thing to deal with this exception is that,
1. Your application should use content:// Uri instead of file:// Uri so that the platform can extend temporary permission for the receiving app to access the resource.
2. Or you need to use FileProvider.
3. The work around from code can be done as follows:
Just add the below code in your Application file's onCreate method as follows:
public class ApplicationMain extends Application{
@Override
public void onCreate() {
super.onCreate();
....
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
....
}
}
Note: This exception is only thrown for applications targeting N or higher. Applications targeting earlier SDK versions are allowed to share file:// Uri, but it's strongly discouraged.
That's all.
Share and comment if any issues.
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/Camera/img_21032017190911.jpg exposed beyond app through ClipData.Item.getUri()
The error or exception is thrown when an application exposes a file:// Uri to another app.
Thus using file:// Uri is discouraged in this scenario because the receiving app may not have access to the path or the file:// Uri shared with it.
Like for example, the receiving app may not have requested the READ_EXTERNAL_STORAGE run time permission, or the platform may be sharing the file:// Uri across user profile boundaries, then this may result in an unexpected behaviour at best or at worst, it may result in a crash.
So the first thing to deal with this exception is that,
1. Your application should use content:// Uri instead of file:// Uri so that the platform can extend temporary permission for the receiving app to access the resource.
2. Or you need to use FileProvider.
3. The work around from code can be done as follows:
Just add the below code in your Application file's onCreate method as follows:
public class ApplicationMain extends Application{
@Override
public void onCreate() {
super.onCreate();
....
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
....
}
}
Note: This exception is only thrown for applications targeting N or higher. Applications targeting earlier SDK versions are allowed to share file:// Uri, but it's strongly discouraged.
That's all.
Share and comment if any issues.
Slots Casinos Near Me - MapYRO
ReplyDeleteDiscover the best slot 동해 출장샵 machines 부천 출장안마 and gaming apps in the 광주광역 출장마사지 United States today. The best casinos in the United States, 여주 출장마사지 however, aren't as 영천 출장마사지 popular with new players as