Home > Enterprise >  Share image using Intent in Android Recycler View
Share image using Intent in Android Recycler View

Time:09-28

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related