如何在 Android 中从 recyclerView 捕获照片 - Kotlin 语言

How to capture photo from recyclerView in Android - Kotlin language

我的任务很简单。在 recyclerView 中,当我点击任何按钮时,我想启动相机,拍照,然后拍摄这张照片。但是我找不到任何解决方案。我在 RecyclerAdapter.kt:

中尝试过的
  inner class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
        var textView1: TextView = itemView.findViewById(R.id.firma_textView1)

        init {
            textView1.setOnClickListener {
                capturePhoto(context, activity)
            }
        }
    }

    fun capturePhoto(context: Context, activity: Activity) {
        if (getCameraPermission(context)) {
            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(activity, cameraIntent, FirstFragment.CAMERA_REQUEST_CODE, null)
        } else {
            ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), FirstFragment.CAMERA_REQUEST_CODE)
        }
    }

    private fun getCameraPermission (context: Context):Boolean {
        return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
    }

使用这段代码我可以启动相机,拍照,但是在 RecyclerAdapter 中没有办法捕捉拍摄的图像。

在Fragment中抓图的正常方式是这样的:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var buttonCapturePhoto = view.findViewById<Button>(R.id.button)
        buttonCapturePhoto.setOnClickListener {
            capturePhoto()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CAMERA_REQUEST_CODE) {
            print("photo captured")
        }
    }

    private fun capturePhoto() {
        if (getCameraPermission(requireContext())) {
            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE)
        } else {
            ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
        }
    }

    private fun getCameraPermission (context: Context):Boolean {
        return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
    }

我也在 Android 开发者页面找到这篇文章 - https://developer.android.com/training/basics/intents/result

他们建议创建 class MyLifecycleObserver 并在 Fragment 中使用它,但我无法在 RecycleAdapter 中使用此代码

lateinit var observer : MyLifecycleObserver

    override fun onCreate(savedInstanceState: Bundle?) {
        // ...

        observer = MyLifecycleObserver(requireActivity().activityResultRegistry)
        lifecycle.addObserver(observer)
    }

我在 activityResultRegistrylifecycle

遇到错误

我还创建了这个 git 存储库来测试:https://github.com/Katzzer/recyclerViewPhotoCaptureKotlinAndroid

您应该将点击事件传递给 fragment/activity

  1. 在您的适配器中创建自定义 interface
  2. 将其传递给适配器构造函数

// pass listener into constructor
class RecyclerAdapter(
  val list:List<YourItemClass>,
  val listener: OnItemClickListener) : ... {
  
  // create a custom listener
  interface OnItemClickListener{
    fun onItemClick(view:View, position:Int)
   }
  
  inner class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {

        // create function bind instead of using init block
        fun bind(item:YourItemClass){
            val textView1: TextView = itemView.findViewById(R.id.firma_textView1)
            // if you want to change image in your ImageView , you could also pass 
            // your ImageView too
            val imgView: ImageView = itemView.findViewById(R.id.imgView)
            textView1.setOnClickListener { view ->
                // this is just an example , but you get the idea

                // listen click event and pass view and position
                listener.onItemClick(view, adapterPosition)
                // or
                listener.onItemClick(imgView, adapterPosition)
            }
        }
    }

   ...
   override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = list[position]
        // bind here
        holder.bind(item)
    }
  ...
}

  1. 已在 Fragment/Activity
  2. 中初始化您的适配器
  3. Fragment/Activity
  4. 中监听点击事件
...
adapter = RecyclerAdapter(list, object : RecyclerAdapter.OnItemClickListener{
   override fun onItemClick(view:View, position:Int){
     // Listen your click event here
     capturePhoto().also { result ->
         // do something

         // dont forget to call notifyItem if you want to update an item in 
         // RecyclerView
         adapter.notifyItemChanged(position)
      }
   }
}
recyclerView.adapter = adapter
...
  1. Fragment\Activity
  2. 中创建您的 capturePhoto 函数