【问题标题】:Share image using Intent in Android Recycler View在 Android Recycler View 中使用 Intent 共享图像
【发布时间】:2021-11-16 19:06:54
【问题描述】:

这是我的数据类:-

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

这是我的分享功能:- 在这里,我将用户作为输入传递

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

我无法分享图片。每次都会出现投射错误。 请帮忙解决这个问题

【问题讨论】:

标签: android image kotlin android-intent android-recyclerview


【解决方案1】:

我认为您没有正确解析可绘制文件。 基本上drawable是“用户”中的一个int值,你不能将它解析为uri。 check this one for reference

【讨论】:

    【解决方案2】:

    这对我有用:

     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 传递了上下文值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2017-08-19
      相关资源
      最近更新 更多