简体   繁体   English

我正在调用 startActivityForResult 并发现未调用 onActivityResult

[英]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:两种可能性:

  1. 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

  1. request code is < 0, will not trigger 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.

相关问题 startActivityForResult onActivityResult in片段“未调用” - startActivityForResult onActivityResult in a fragment “not called” 在 startActivityForResult 上立即调用 onActivityResult - onActivityResult called immediately on startActivityForResult Android:onActivityResult()未调用startActivityForResult - Android: onActivityResult() is not calling for startActivityForResult Android:startActivityForResult没有调用onActivityResult - Android: startActivityForResult not calling onActivityResult 不为startActivityForResult调用onActivityResult - onActivityResult isn't called for startActivityForResult Android startActivityForResult,setResult,onActivityResult未调用 - Android startActivityForResult, setResult, onActivityResult not called 侦听器内部的startactivityforresult()未调用onActivityResult() - startactivityforresult() inside a listener not calling onActivityResult() 调用startActivityForResult永远不会传递给onActivityResult - Calling startActivityForResult never pass to onActivityResult 从onActivityResult调用新的StartActivityForResult - Calling a new StartActivityForResult from onActivityResult 返回startActivityForResult而不调用onActivityResult - startActivityForResult returning without calling onActivityResult
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM