我目前正在使用防火墙存储允许用户上传图像。我复制了我以前正在工作的java代码,并试图将它转换为Kotlin,但我有一个不匹配的问题。错误信息是:
需要类型不匹配: Uri找到: Uri?
这是我的密码。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 1046) {
try {
val imageUri : Uri? = data?.data
val imageStream: InputStream? = this.contentResolver.openInputStream(imageUri) //this is the line that errors imageUri has a mismatch error
val selectedImage = BitmapFactory.decodeStream(imageStream)
CloudStorage().upload(imageUri,
{ s ->
uploadedImageURL = s
}) { e ->
Toast.makeText(this@CreatePostActivity, e.message, Toast.LENGTH_SHORT).show()
e.printStackTrace()
}
} catch (e: FileNotFoundException) {
e.printStackTrace()
Toast.makeText(this, "file not found", Toast.LENGTH_SHORT).show()
}
}
}谢谢!
发布于 2020-11-03 17:39:01
imageUri不能为空,因为openInputStream不接受可空值。如果该值不可为空,则只能执行代码来修复它,例如:
data?.data?.let { imageUri ->
try {
val imageStream: InputStream? = this.contentResolver.openInputStream(imageUri)
val selectedImage = BitmapFactory.decodeStream(imageStream)
CloudStorage().upload(imageUri,
{ s ->
uploadedImageURL = s
}) { e ->
Toast.makeText(this@CreatePostActivity, e.message, Toast.LENGTH_SHORT).show()
e.printStackTrace()
}
} catch (e: FileNotFoundException) {
e.printStackTrace()
Toast.makeText(this, "file not found", Toast.LENGTH_SHORT).show()
}
} ?: notifyUserImageUriIsNull()请注意,您要调用super.onActivityResult()两次。
发布于 2020-11-04 01:42:53
"Android“给出了答案。

https://stackoverflow.com/questions/64667718
复制相似问题