从 Intent Multiple 选择 1 张图像时出错
Error when selecting 1 image from Intent Multiple
下面的代码在 select 多张图片时工作正常,但是如果 selected 只有一张图片,它会给我一个错误。这是错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1110896904, result=-1, data=Intent
这是代码
@SuppressLint("IntentReset")
private fun checkAccessAndUpload() {
if (context?.let {
EasyPermissions.hasPermissions(
it,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
} == true) {
if (Build.VERSION.SDK_INT < 19) {
var intent = Intent()
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(
Intent.createChooser(intent, "Select Picture")
, PICK_IMAGE_MULTIPLE
)
} else {
var intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, PICK_IMAGE_MULTIPLE)
}
} else {
EasyPermissions.requestPermissions(
this,
"We need to access your camera and storage to upload your pictures",
123,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK && null != data) {
val count = data.clipData!!.itemCount
for (i in 0 until count) {
var imageUri: Uri = data.clipData!!.getItemAt(i).uri
ImageList.add(imageUri)
}
uploadImage()
}
}
错误发生在这里
val count = data.clipData!!.itemCount
如何修复此错误,以便我的图库意图可以 select 1 或多个
像这样更改代码
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK && null != data) {
if(data.clipData != null) {
val count = data.clipData!!.itemCount
for (i in 0 until count) {
var imageUri: Uri = data.clipData!!.getItemAt(i).uri
ImageList.add(imageUri)
}
}else if(data.data != null) {
var imageUri: Uri = data.data!!
ImageList.add(imageUri)
}
uploadImage()
}
}
下面的代码在 select 多张图片时工作正常,但是如果 selected 只有一张图片,它会给我一个错误。这是错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1110896904, result=-1, data=Intent
这是代码
@SuppressLint("IntentReset")
private fun checkAccessAndUpload() {
if (context?.let {
EasyPermissions.hasPermissions(
it,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
} == true) {
if (Build.VERSION.SDK_INT < 19) {
var intent = Intent()
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(
Intent.createChooser(intent, "Select Picture")
, PICK_IMAGE_MULTIPLE
)
} else {
var intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, PICK_IMAGE_MULTIPLE)
}
} else {
EasyPermissions.requestPermissions(
this,
"We need to access your camera and storage to upload your pictures",
123,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK && null != data) {
val count = data.clipData!!.itemCount
for (i in 0 until count) {
var imageUri: Uri = data.clipData!!.getItemAt(i).uri
ImageList.add(imageUri)
}
uploadImage()
}
}
错误发生在这里
val count = data.clipData!!.itemCount
如何修复此错误,以便我的图库意图可以 select 1 或多个
像这样更改代码
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK && null != data) {
if(data.clipData != null) {
val count = data.clipData!!.itemCount
for (i in 0 until count) {
var imageUri: Uri = data.clipData!!.getItemAt(i).uri
ImageList.add(imageUri)
}
}else if(data.data != null) {
var imageUri: Uri = data.data!!
ImageList.add(imageUri)
}
uploadImage()
}
}