首页 > 解决方案 > 仅在 Android 一台设备上使用相机拍照后变量变为空或为空

问题描述

因此,由于客户端提出的流请求,我现在使用控制器类在我的 Activity 中拍摄和显示图片。图片将被放入 ArrayList 和 RecyclerView 中。这是我拍摄照片的方式:

方法一

我的控制器类:

companion object{
    var runnable:Runnable? = null
    var imagePath:String? = null
    var index = 0 //To mark which ArrayList index I would like to put the taken picture
}

fun init(){
    runnable = Runnable{
        //get image and put it to the Imageview
        arrayList[index].image = imagePath
        recyclerView.adapter!!.notifyDataSetChanged()
    }
}

然后我会在我的活动中运行该可运行文件,例如:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Controller.imagePath = FileUtils.getPath(this, data.getData()!!)
    Controller.runnable.run()
}

方法二

这次我不使用静态变量,除了一个 Int 变量来标记 ArrayList 的索引,我将在其中将图像添加到其项目中。

控制器类:

companion object{
    var index = 0
}

fun updateImage(data:Intent,position:Int){
    val imagePath = FileUtils.getPath(this, data.getData()!!)
    arrayList[position].image = imagePath
    recyclerView.adapter!!.notifyDataSetChanged()
}

然后我会从我的活动中使用该功能,例如:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    controller.updateImage(data!!,controller.index)
}

结果:

这两种设备在我尝试过的任何设备上都可以正常工作,除了 Android One 设备,即 Mi A1。拍照后由于某种原因,使用方法1,当相机关闭时,runnable会突然为空,然后崩溃。而对于METHOD 2,ArrayList会突然变空,使用相机拍照后大小为0,因此也会崩溃。

实际上,崩溃有点随机,有时很好,有时会崩溃。但主要是崩溃。

标签: androidimagecameracrash

解决方案


推荐阅读