Search code examples
androidonactivityresult

onActivityResult always got -1 as resultcode


I try to use MediaProjectionManager to capture a screenshot. The first step I take is typing following lines of code:

MediaProjectionManager projectionManager = (MediaProjectionManager)this.getContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(), 1);

Then I expect to get responded with the onActivityResult(int requestCode, int resultCode, Intent data) function. However I find that the resultCode is always -1 and the data always contains null uri and null mData. Its like there is no image content passed to onActivityResult.

Does anyone know what caused this issue or how to retrieve the screenshot from the data?

Any help is much appreciated!


Solution

  • Just take a look to the Activity.RESULT_OK.

    public static final int RESULT_OK = -1;
    

    So if you get a -1 in your resultCode you're good.

    Next after reading the MediaProjectionManager documentation

    I read that you need to call getMediaProjection. Try to call getMediaProjection method in your onActivityResult and give it the resultCode and resultData.

    To get a basic sample, look this code => MediaProjectionManager use

    Hope it can help you !!