dj43
dj43

Reputation: 73

Use image picker inside recyclerview

I am using image picker inside recyclerview

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.tvAnimalType.text = items[position].name
    holder.image.setOnClickListener{
        requestPermissions(it.context as Activity , arrayOf(WRITE_EXTERNAL_STORAGE),1)
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "image/*"
        (it.context as Activity).startActivityForResult(intent, 1)
    }
}

In Main Activity

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
        1 -> {/*file front*/
            if (resultCode == Activity.RESULT_OK && data != null) {
                val selectedImageUri = data.data as Uri
                val selectedImageBitmap: Bitmap =
                    MediaStore.Images.Media.getBitmap(this.contentResolver, selectedImageUri)
            }
        }
    }
}

My question is how to load the selected image in recylerview imageview ?

Upvotes: 1

Views: 806

Answers (1)

Jakir Hossain
Jakir Hossain

Reputation: 3930

Try like the following

Pass item position as request_code.

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.tvAnimalType.text = items[position].name

    // here you can get image as bitmap using filepath and set in to your image view
    val filepath = items[position].filePath
    val bitmapImage = ...........[get bitmap from filepath]
    holder.image.setImageBitmap(bitmapImage)

    holder.image.setOnClickListener{
        requestPermissions(it.context as Activity , arrayOf(WRITE_EXTERNAL_STORAGE),1)
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "image/*"
        (it.context as Activity).startActivityForResult(intent, position) // pass position as request code
    }
}

And In activity do like the following

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
            if (resultCode == Activity.RESULT_OK && data != null) {
                val selectedImageUri = data.data as Uri
                val selectedImageBitmap: Bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, selectedImageUri)

                // here you can get image file path using URI
                val filePathFromURI = ......[get image path using URI]

                // then save it to your list data like
                items[requestCode].filePath = filePathFromURI // here items is your data set which passed in adapter from your activity you should replace it with yours.
                // now you have to notify your adapter that your data set is changed
                adapter.notifyDataSetChanged() // replace adapter with yours.

            }
    }
}

And one important thing you should have a property named filePath in your data class.

Upvotes: 1

Related Questions