Kotlin中如何将拍摄的照片放入RecyclerView

How to put the taken photos in RecyclerView in Kotlin

我的应用程序使用相机拍照,然后将它们保存在 MediaStore 中。我想使用 Glide 将这些图片放在我的 RecyclerView 中,但我不知道该怎么做。

保存图片的函数:

private fun imageCapture() {

        // Set desired name and type of captured image
        val contentValues = ContentValues().apply {
            put(MediaStore.MediaColumns.DISPLAY_NAME, "${what_is_that_insect_tv.text}")
            put(MediaStore.MediaColumns.DATE_MODIFIED.format("MM/dd/yyyy"), (Calendar.getInstance().timeInMillis / 1000L))
            put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
        }

        // Create the output file option to store the captured image in MediaStore
        val outputFileOptions = ImageCapture.OutputFileOptions
            .Builder(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
            .build()

        // Initiate image capture
        imageCapture?.takePicture(
            outputFileOptions,
            cameraExecutor,
            object : ImageCapture.OnImageSavedCallback {
                override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
                    // Image was successfully saved to `outputFileResults.savedUri`
                }

                override fun onError(exception: ImageCaptureException) {
                    val errorType = exception.imageCaptureError
                    Toast.makeText(requireContext(), "$errorType", Toast.LENGTH_SHORT).show()
                }
            })
    }

适配器中的函数

fun bind(insect: Insect){
           with(itemView){
               name_insect_item.text = insect.name
               Glide.with(this)
                   .load()
                   .into(this.image_insect_item)
           }
       }

我在我的代码中这样做:

 Glide.with(holder.imageView.getContext())
     .load(new File(hotel.imageId2))
     .into(holder.imageView)

这也可能有帮助:

RecyclerView- Glide

为了使用 Glide 你必须获得保存图像的 URI,在 onImageSaved 我相信你可以像这样调用 .getSavedUri() :

override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
                val uri = ImageCapture.OutputFileResults.getSavedUri()
            }

然后您应该能够在提供给适配器的 ArrayList 中使用 Uri。从这里得到它:

https://developer.android.com/reference/androidx/camera/core/ImageCapture.OutputFileResults#getSavedUri()