[英]I'm calling startActivityForResult and finding that onActivityResult is not called
I'm trying to choose an image from the gallery and apply it to a text view.我正在尝试从图库中选择一张图片并将其应用于文本视图。
I have made a call to startActivityForResult() function and override onActivityResult() function but on testing I have found that onActivityResult() is not getting called.我已经调用了 startActivityForResult() 函数并覆盖了 onActivityResult() 函数,但是在测试中我发现没有调用 onActivityResult() 。 What Would be the reason for this?这是什么原因?
It has super.onActivityResult(requestCode, resultCode, data)它有 super.onActivityResult(requestCode, resultCode, data)
var intent: Intent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, IMG_RESULT)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//I made this toast to see if the program arrived to onActivityResult but it never showed
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show()
if (resultCode == Activity.RESULT_OK)
if (data != null) {
val contentURI = data!!.data
try {
val bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, contentURI)
val path = saveImage(bitmap)
Toast.makeText(this@MainActivity, "Image Saved!", Toast.LENGTH_SHORT).show()
image_blank!!.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
Toast.makeText(this@MainActivity, "Failed!", Toast.LENGTH_SHORT).show()
}
}
}
fun saveImage(myBitmap: Bitmap): String {
When using android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
there is no guarantee that the user will pick an image.使用android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
时,不能保证用户会选择图像。
The solution below will limit the user to picking an image and does not specify from which location it'll be done - the user is free to navigate and choose what he wants.下面的解决方案将限制用户选择图像并且没有指定从哪个位置完成 - 用户可以自由导航并选择他想要的。
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/jpeg", "image/png", "image/bmp"))
startActivityForResult(intent, IMG_RESULT)
This exact solution works in my project and should be fine out of the box for you as well.这个确切的解决方案在我的项目中有效,对你来说也应该是开箱即用的。
Two possibilities:两种可能性:
onActivityResult()
is never invoked if your activity sets noHistory to true.如果您的活动将 noHistory 设置为 true,则永远不会调用onActivityResult()
。 android:noHistory="true"
This method is never invoked if your activity sets noHistory to true.如果您的活动将 noHistory 设置为 true,则永远不会调用此方法。 https://developer.android.com/reference/android/app/Activity.html#startActivityForResult https://developer.android.com/reference/android/app/Activity.html#startActivityForResult
onActivityResult()
(could not find it documented anywhere, but as per the comments from many fellow developers).请求代码 < 0,不会触发onActivityResult()
(无法在任何地方找到它的文档,但根据许多开发人员的评论)。requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits, as described in startActivityForResult(Intent, int). requestCode int:如果 >= 0,该代码将在活动退出时在 onActivityResult() 中返回,如 startActivityForResult(Intent, int) 中所述。
If you are calling startActivityForResult
from a fragment
be sure that the parent activity will propagate the result.如果您从fragment
调用startActivityForResult
,请确保父活动将传播结果。
In your Activity在你的活动中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case A_REQUEST_CODE_USED_IN_YOUR_ACTIVITY:
...
break;
case ANOTHER_REQUEST_CODE_USED_IN_YOUR_ACTIVITY:
...
break;
default:
// this will propagate the result to your Fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
In your Fragment在你的片段中
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IMG_RESULT:
if (resultCode = RESULT_OK) {
// put here your code
...
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.