【问题标题】:Problem with image sharing intent in KotlinKotlin 中的图像共享意图问题
【发布时间】:2018-11-08 08:23:52
【问题描述】:

我在这里分享图像意图。图像位于可绘制文件夹中。 给这个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)

代码:

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 "))
}

【问题讨论】:

标签: android kotlin


【解决方案1】:

根据Android文档,你应该使用FileProvider

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

但是当我尝试时,它可以在某些设备上运行,而不能在其他设备上运行。

所以对我来说一个安全的方法是先将文件导出到某个外部存储 (Environment.getExternalStorageDirectory),然后共享给其他应用。这适用于我的所有设备。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多