Use MediaProjection to get Screen Capture.
https://github.com/shark5/ScreenCapture
如果对你有帮助,请Star,谢谢!
compile 'com.shark:screencapture:1.0.0'
ScreenCapture mScreenCapture = ScreenCapture.newInstance(this);
mScreenCapture.setCaptureListener(new ScreenCapture.OnCaptureListener() {
@Override
public void onScreenCaptureSuccess(Bitmap bitmap, String savePath) {
Log.d(TAG, "onScreenCaptureSuccess savePath:" + savePath);
}
@Override
public void onScreenCaptureFailed(String errorMsg) {
Log.d(TAG, "onScreenCaptureFailed errorMsg:" + errorMsg);
}
@Override
public void onScreenRecordStart() {
Log.d(TAG, "onScreenRecordStart");
}
@Override
public void onScreenRecordStop() {
Log.d(TAG, "onScreenRecordStop");
}
@Override
public void onScreenRecordSuccess(String savePath) {
Log.d(TAG, "onScreenRecordSuccess savePath:" + savePath);
}
@Override
public void onScreenRecordFailed(String errorMsg) {
Log.d(TAG, "onScreenRecordFailed errorMsg:" + errorMsg);
}
});
可不设置,即使用默认路径
mScreenCapture.setImagePath(Environment.getExternalStorageDirectory().getPath() + "/ScreenCapture/screen_capture/", "image_screen.png");
mScreenCapture.setRecordPath(Environment.getExternalStorageDirectory().getPath() + "/ScreenCapture/record/", "recording_screen.mp4");
if (mScreenCapture != null) {
mScreenCapture.screenCapture();
}
if (mScreenCapture != null) {
mScreenCapture.record();
}
/**
* Handle permission here which caused by MediaProjectionManager.createScreenCaptureIntent()
* @param requestCode
* @param resultCode
* @param data
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mScreenCapture != null) {
mScreenCapture.onActivityResult(requestCode, resultCode, data);
}
}
/**
* Handle permission here. Like Manifest.permission.WRITE_EXTERNAL_STORAGE
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (mScreenCapture != null) {
mScreenCapture.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mScreenCapture != null) {
mScreenCapture.cleanup();
mScreenCapture = null;
}
}