android - 仅在 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,因此也会崩溃。
实际上,崩溃有点随机,有时很好,有时会崩溃。但主要是崩溃。
解决方案
推荐阅读
- linux - Raspbian 启动时自动运行命令
- algorithm - 寻找最长路径网格
- reactjs - 处理 ag grid react 并渲染复选框网格
- google-cloud-datastore - Most efficient way to delete thousands of entities in datastore
- postgresql - Postgresql如何在事务中使用多个存储过程
- pointers - 如果对象在切片中,方法不会改变对象的值
- asp.net-web-api - 在 Dynamics CRM 中使用 C# 和 WEB API 批量插入
- python - TensorFlow numpy 重复
- sql-server - 有人可以转换代码以使其使用 EXEC @sql 而不是 EXEC sp_executesql 吗?
- python - 安装 discord.py 1.0 时遇到问题