java - 设置两个单独的图像视图的图像
问题描述
我的应用程序中有两个 imageView,我有两个按钮。每个 Button 将使用 Gallery Picker 更改其中一个 ImageView。
但是我不知道如何分别为每个 imageViews 选择图像。
lateinit var frontImageView: ImageView
lateinit var backImageView: ImageView
private val pickImage = 100
private var imageUri: Uri? = null
...
override fun onCreate(savedInstanceState: Bundle?) {
frontImageView = findViewById(R.id.cb_image_frontside)
backImageView = findViewById(R.id.cb_image_backside)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == pickImage) {
imageUri = data?.data
frontImageView.setImageURI(imageUri) // This will just change the image for "frontImageView"
// But I can only always have one of the
// imageViews changed at one time
// How can I change the Image of "backImageView" with
// the other button?
}
}
// This is the fuction for the buttons
fun pickMedia(view: View) {
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
startActivityForResult(gallery, pickImage)
}
解决方案
根据您上面的代码,您可以尝试这样的事情:
lateinit var frontImageView: ImageView
lateinit var backImageView: ImageView
lateinit var frontButton: AppCompatButton
lateinit var backButton: AppCompatButton
private val pickFrontImage = 100
private val pickBackImage = 101
private var imageUri: Uri? = null
...
override fun onCreate(savedInstanceState: Bundle?) {
frontImageView = findViewById(R.id.cb_image_frontside)
backImageView = findViewById(R.id.cb_image_backside)
frontButton = findViewById(R.id.frontButton)
backButton = findViewById(R.id.backButton)
settButtonListeners()
}
private fun settButtonListeners() {
frontButton.setOnClickListener {
pickMedia(pickFrontImage)
}
backButton.setOnClickListener {
pickMedia(pickBackImage)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
imageUri = data?.data
if (requestCode == pickFrontImage) {
frontImageView.setImageURI(imageUri) // This will just change the image for "frontImageView"
// But I can only always have one of the
// imageViews changed at one time
} else if (requestCode == pickBackImage) {
backImageView.setImageURI(imageUri) // This will just change the image for "frontImageView"
// But I can only always have one of the
// imageViews changed at one time
}
}
}
// This is the fuction for the buttons
fun pickMedia(requestCode: Int) {
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
startActivityForResult(gallery, requestCode)
}
在pickMedia
函数上将请求的代码作为参数传递。
推荐阅读
- xml - xslt 根据标准和 groupBy 删除记录
- php - Wordpress 出现问题,在主目录中生成随机文件和代码
- angular - 无法在新电脑上使用 ionic - 无法为空白项目提供服务
- generative-adversarial-network - NiftyNet 中的 GAN 调节输入
- flutter - 用大视频慢摇咀嚼
- spring-boot - Dockerized Spring boot 应用程序连接到数据库 docker 映像
- spring - 获取给定 Spring Bean 的类注解
- sql - 尝试删除不存在的记录时使 SQL 查询失败
- ionic4 - 如何在ionic 4中仅显示一次同意页面
- c# - HANDLE 和 CRITICAL_SECTION 的 C# 编组