0

I have images in external storage in Orders folder and that image i am trying to share to external apps using intent

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Orders");
String filename = "OrderID.jpg";
File file = new File(file,filename);
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("image/*");
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
                try {
                     startActivity(intent);
                } catch (android.content.ActivityNotFoundException ex) {

                }

I have given storage permission on loading application itself but i get every time app crashes

ndroid.os.FileUriExposedException: file:///storage/emulated/0/Orders/OrderID.jpg exposed beyond app through ClipData.Item.getUri()

Can any one help me how i cant fix this ? i have followed many post in google but no use

android.os.FileUriExposedException: file.jpg exposed beyond app through ClipData.Item.getUri()

https://github.com/react-native-community/react-native-share/issues/185

https://github.com/tungdx/android-media-picker/issues/11

https://github.com/MLSDev/RxImagePicker/issues/13

scott
  • 3,112
  • 19
  • 52
  • 90

1 Answers1

4

You must use FileProvider when you want to share files to other apps.

check https://developer.android.com/training/secure-file-sharing/setup-sharing.

use FileProvider.getUriForFile to get Uri instead of Uri.fromFile(file) in you code.

FileProvider.getUriForFile(this,"your_package.fileprovider",photoFile);
Man
  • 2,720
  • 2
  • 13
  • 21