-3

here I'm sharing the image intent. The image is in a drawable folder. Giving this excpetion. android.os.FileUriExposedException:

file:///storage/emulated/0/Android/data/com.example.arkkhano.myapplication/cache/myImage.png exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2356)

The code:

sharing_img.setOnClickListener {
    val myDrawable = tv_view_home2e.drawable
    val bitmap = (myDrawable as BitmapDrawable).bitmap
    val file = File(externalCacheDir,"myImage.png")
    val fOut = FileOutputStream(file)
    bitmap.compress(Bitmap.CompressFormat.PNG,90,fOut)
    fOut.flush()
    fOut.close()
    file.setReadable(true,false)
    val intent = Intent(Intent.ACTION_SEND)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file))
    intent.type = "image/png"
    intent.putExtra(Intent.EXTRA_SUBJECT,"Subject here")
    startActivity(Intent.createChooser(intent,"Share to "))
}
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196

1 Answers1

0

According to Android document, you should use FileProvider

https://developer.android.com/reference/android/support/v4/content/FileProvider

but when I tried, it works on some device and doesn't work on other devices.

So a safe way for me is export the file to somewhere external storage (Environment.getExternalStorageDirectory) first, and share to other apps. That works for all my devices.

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13