简体   繁体   中英

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!

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

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;


        }
    }
}

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