Reputation: 121
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
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
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