Android 图像共享无法在所有谷歌像素设备上运行

Android 图像共享无法在所有谷歌像素设备上运行,android,android-intent,android-sharing,Android,Android Intent,Android Sharing,我正在使用intent共享应用程序中的图像。它在所有设备上都能正常工作,但当涉及到谷歌Pixel 2 XL时,图像不会加载到skype和gmail上。在Gmail上,它说:无法附加文件。下面是代码片段 private fun shareImageFromURI(url: String?) { Glide.with(this) .asBitmap() .load(url) .into(object : CustomTarget<Bit

我正在使用intent共享应用程序中的图像。它在所有设备上都能正常工作,但当涉及到谷歌Pixel 2 XL时,图像不会加载到skype和gmail上。在Gmail上,它说:无法附加文件。下面是代码片段

 private fun shareImageFromURI(url: String?) {
    Glide.with(this)
        .asBitmap()
        .load(url)
        .into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                val intent = Intent(Intent.ACTION_SEND)
                intent.type = "image/*"
                intent.putExtra(Intent.EXTRA_STREAM, getBitmapFromView(resource))
                startActivity(Intent.createChooser(intent, "Share Image"))
            }

            override fun onLoadCleared(placeholder: Drawable?) {

            }
        })
}

fun getBitmapFromView(bmp: Bitmap?): Uri? {
    var bmpUri: Uri? = null
    try {
        val file = File(this.externalCacheDir, System.currentTimeMillis().toString() + ".jpg")

        val out = FileOutputStream(file)
        bmp?.compress(Bitmap.CompressFormat.JPEG, 90, out)
        out.close()
        bmpUri = Uri.fromFile(file)

    } catch (e: IOException) {
        e.printStackTrace()
    }
    return bmpUri
}
private-fun-shareImageFromURI(url:String?){
用(这个)滑翔
.asBitmap()
.load(url)
.into(对象:CustomTarget(){
覆盖资源就绪(资源:位图,转换:转换?){
val intent=intent(intent.ACTION\u发送)
intent.type=“image/*”
intent.putExtra(intent.EXTRA_流,getBitmapFromView(资源))
startActivity(Intent.createChooser(Intent,“共享图像”))
}
覆盖已清除(占位符:可绘制?){
}
})
}
有趣的getBitmapFromView(bmp:Bitmap?):Uri?{
var bmpUri:Uri?=null
试一试{
val file=file(this.externalCacheDir,System.currentTimeMillis().toString()+“.jpg”)
val out=FileOutputStream(文件)
bmp?.compress(位图.CompressFormat.JPEG,90,输出)
结束
bmpUri=Uri.fromFile(文件)
}捕获(e:IOException){
e、 printStackTrace()
}
返回bmpUri
}
尝试以下解决方案:

        Glide.with(this)
        .asBitmap()
        .load(url)
        .into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                val imgBitmapPath = MediaStore.Images.Media.insertImage(
                    this@ImagesActivity.contentResolver,
                    resource,
                    "eVitalRx_Greetings_" + Calendar.getInstance().getTime(),
                    null
                )
                val imgUri = Uri.parse(imgBitmapPath)
                val intent = Intent(Intent.ACTION_SEND)
                intent.type = "image/*"
                intent.putExtra(Intent.EXTRA_STREAM, imgUri)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                try {
                    startActivity(Intent.createChooser(intent, ApplicationClass.languageJson?.downloadDialog?.shareImage))
                }catch (e:Exception){
                    this@ImagesActivity.Toast("Operation Failed")

                }
            }

            override fun onLoadCleared(placeholder: Drawable?) {

            }
        })
Glide.with(这个)
.asBitmap()
.load(url)
.into(对象:CustomTarget(){
覆盖资源就绪(资源:位图,转换:转换?){
val imgBitmapPath=MediaStore.Images.Media.insertImage(
this@ImagesActivity.contentResolver,
资源,,
“eVitalRx\u问候语”+Calendar.getInstance().getTime(),
无效的
)
val imgUri=Uri.parse(imgBitmapPath)
val intent=intent(intent.ACTION\u发送)
intent.type=“image/*”
intent.putExtra(intent.EXTRA_流,imgUri)
intent.addFlags(intent.FLAG\授予\读取\ URI\权限)
试一试{
startActivity(Intent.createChooser(Intent,ApplicationClass.languageJson?.downloadDialog?.shareImage))
}捕获(e:例外){
this@ImagesActivity.Toast(“操作失败”)
}
}
覆盖已清除(占位符:可绘制?){
}
})

非常感谢。它工作得非常好:)