在Android recycle View中使用Intent共享图像



这是我的数据类:-

data class User(val imagepic: Int)

将值传递给回收器视图:-

users.add(User(R.drawable.splash_bc))
users.add(User(R.drawable.image))
users.add(User(R.drawable.splash_bc))
users.add(User(R.drawable.share))
users.add(User(R.drawable.splash_bc))
users.add(User(R.drawable.image))

这是我的共享功能:-这里我将user作为Input

传递
fun shareString(user: User) {
var image_path: String
val file : File = File(user)
val uri = Uri.fromFile(file)
val intent = Intent(Intent.ACTION_SEND)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(itemView.context, Intent.createChooser(intent,"Share to :"),null)

}

我无法共享图像。每次都出现类型转换错误。请帮忙解决这个问题

我不认为你是解析可绘制文件正确的方式。drawable是一个int值在User"中,你不能把它解析成uri。查看这个参考

这是为我工作:

fun shareString(context: Context,user: User) {
val b = BitmapFactory.decodeResource(context.resources, user.mImage)
val share = Intent(Intent.ACTION_SEND)
share.type = "image/jpeg"
val path = MediaStore.Images.Media.insertImage(context.contentResolver, b, "Title", null)
val imageUri: Uri = Uri.parse(path)
share.putExtra(Intent.EXTRA_STREAM, imageUri)
startActivity(context,Intent.createChooser(share, "Select"),null)
}

我已经从mainActivity传递了上下文值。

最新更新