user1870797
user1870797

Reputation: 121

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!

Upvotes: 0

Views: 204

Answers (2)

jigar savaliya
jigar savaliya

Reputation: 504

requestcode is always -1 if it give some output you have to check resultcode and is 1.like this

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case 1:
                    //do your stuff
                break;


        }
    }
}

Upvotes: 0

olivejp
olivejp

Reputation: 929

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 !!

Upvotes: 3

Related Questions