-1

This is my data class :-

data class User(val imagepic: Int)

Passing value to recycler view :-

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

this is my share function :- Here I am passing user as 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)
            
        }

I am not able to share image. getting casting error everytime. Please help on this

Rahul
  • 117
  • 1
  • 14

2 Answers2

0

I don't think you are parsing the drawable file right way. basically drawable is an int value in "User", You can't parse that to uri. check this one for reference

subrata sharma
  • 344
  • 4
  • 17
0

This is working for me :

 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)

        }

I have pass context value from mainActivity.

Rahul
  • 117
  • 1
  • 14