I am trying to share a photo, I successfully create an 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;
}
I click Share icon and the list of share options apears - that's OK. As soon as I click the Whatsapp icon for example I get the Exception:
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)
What am I doing wrong?
Edited: This is not the same problem as the other link suggest and not the same solution needed (See the answer I accepted). Yes it's the same exception but, in the link, he couldn't read the file, I have no problem reading the file and creating the Intent, the crash happened for me when I actually clicked the icon of the App I wanted to share the file with.
If someone gets my scenario and look for solution, I'm not sure he will find the one in the link that easy or even helpful - I didn't.. :)