简体   繁体   中英

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. What Would be the reason for this?

It has 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.

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.

android:noHistory="true"

This method is never invoked if your activity sets noHistory to true. 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).

requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits, as described in startActivityForResult(Intent, int).

If you are calling startActivityForResult from a fragment be sure that the parent activity will propagate the result.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM