Android 在recyclerview中使用图像选择器

Android 在recyclerview中使用图像选择器,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我在recyclerview中使用图像选择器 override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.tvAnimalType.text = items[position].name holder.image.setOnClickListener{ requestPermissions(it.context as Activity , arrayOf(WRITE_EXTER

我在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)
    }
}
主要活动

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)
            }
        }
    }
}
我的问题是如何在recylerview imageview中加载所选图像?

尝试如下操作

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.

            }
    }
}
项目位置
作为
请求代码
传递

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
    }
}
活动中
执行以下操作

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.

            }
    }
}

还有一件很重要的事,您应该在
数据
类中有一个名为
文件路径
的属性。

将单击的位置存储在变量中,并在活动结果中将图像添加到recyclerview列表中的选择位置,并通知适配器。您可以使用interface@devil10你的回答非常有效,非常感谢,还有一个问题,我是否应该对存储权限执行类似的操作?是的,您将您的方法置于活动中,然后通过方法引用从recyclerview调用它。您可以发布您的答案,这样我就可以接受它,并且您可以对该问题进行投票,以便其他用户发现它很有用