尝试分享照片时出现异常
Getting Exception when trying to share a photo
我正在尝试分享照片,我成功创建了一个 Intent:
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
_currentImagePosition = _viewPager.getCurrentItem();
Uri uri = Uri.fromFile((new File(_imageUrls.get(_currentImagePosition))));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
}
我单击“共享”图标,出现共享选项列表 - 没关系。
例如,一旦我单击 Whatsapp 图标,我就会收到异常:
2018-10-26 13:57:39.497 21256-21256/com.eibimalul.smartgallery E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eibimalul.smartgallery, PID: 21256
android.os.FileUriExposedException: file:///storage/0000-0000/DCIM/Camera/20181013_103421.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2356)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:944)
我做错了什么?
已编辑:
这与其他 link 建议的问题不同,所需的解决方案也不同(请参阅我接受的答案)。
是的,这是相同的异常,但是,在 link 中,他无法读取文件,我读取文件和创建 Intent 没有问题,当我实际单击 App I 的图标时,崩溃发生在我身上想与共享文件。
如果有人了解我的情况并寻求解决方案,我不确定他会在 link 中找到那么简单甚至有用的解决方案 - 我没有.. :)
在 Application
class
的 onCreate()
方法中添加以下代码
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
if (Build.VERSION.SDK_INT > 23) {
builder.detectFileUriExposure();
}
我正在尝试分享照片,我成功创建了一个 Intent:
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
_currentImagePosition = _viewPager.getCurrentItem();
Uri uri = Uri.fromFile((new File(_imageUrls.get(_currentImagePosition))));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
}
我单击“共享”图标,出现共享选项列表 - 没关系。 例如,一旦我单击 Whatsapp 图标,我就会收到异常:
2018-10-26 13:57:39.497 21256-21256/com.eibimalul.smartgallery E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eibimalul.smartgallery, PID: 21256
android.os.FileUriExposedException: file:///storage/0000-0000/DCIM/Camera/20181013_103421.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2356)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:944)
我做错了什么?
已编辑: 这与其他 link 建议的问题不同,所需的解决方案也不同(请参阅我接受的答案)。 是的,这是相同的异常,但是,在 link 中,他无法读取文件,我读取文件和创建 Intent 没有问题,当我实际单击 App I 的图标时,崩溃发生在我身上想与共享文件。
如果有人了解我的情况并寻求解决方案,我不确定他会在 link 中找到那么简单甚至有用的解决方案 - 我没有.. :)
在 Application
class
onCreate()
方法中添加以下代码
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
if (Build.VERSION.SDK_INT > 23) {
builder.detectFileUriExposure();
}