我试图在片段中调用registerForActivityResult,当它完成时,它只会关闭片段。
我的问题是如何从片段中调用这个函数?
代码:
private val profileImageLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result != null && result.resultCode == Activity.RESULT_OK) {
profileImageUri = result.data?.data
try {
profileImageUri?.let { profileImageUri ->
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P){
val bitmap: Bitmap = getBitmap(activity?.contentResolver, profileImageUri)
context?.let { context ->
Glide.with(context).load(bitmap).into(pfpIV)
// here it closes the fragment
}
} else {
context?.let { context ->
val source = ImageDecoder.createSource(context.contentResolver, profileImageUri)
var bitmap = ImageDecoder.decodeBitmap(source)
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
Glide.with(context).load(bitmap).into(pfpIV)
// here it closes the fragment
}
}
}
} catch (e:IOException){
e.printStackTrace()
}
}
}发布于 2021-09-04 12:16:31
我找到的唯一解决方案是使用一个活动,似乎当使用registerForActivityResult时,它会将我返回给活动,而不是片段,所以如果您在一个不是活动的主/默认的片段中,您将无法返回到它,函数将返回到活动的主片段,因为它在技术上打开了从用户那里获取信息的新意图(在这种情况下是图像),所以它必须返回到活动,不能返回到片段。
https://stackoverflow.com/questions/69045320
复制相似问题