使用Mediaprojection和Imagereader连续捕获屏幕快照。 但这导致设备无响应。 设备停止工作,只有重启才能使其正常。
请在下面找到代码段。
日志显示连续错误- BufferQueueProducer:[ImageReader-720x1280f1m2-11316-1] dequeueBuffer:BufferQueue已被放弃
是失速装置吗?任何帮助深表感谢。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_cast);
screenShotBtn = findViewById(R.id.capture_screenshot);
screenShotBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scheduleCapture();
}
});
initProc();
initHandlerThread();
}
private void initHandlerThread() {
HandlerThread handlerThread = new HandlerThread("CaptureImage");
handlerThread.start();
Looper looper = handlerThread.getLooper();
mHandler = new Handler(looper);
}
void initProc() {
mMediaProjectManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
startActivityForResult(mMediaProjectManager.createScreenCaptureIntent(),
SCREENSHOT_INTENT_IDENTIFIER);
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = window.getDefaultDisplay();
metrics = new DisplayMetrics();
display.getMetrics(metrics);
Point size = new Point();
display.getSize(size);
mWidth = size.x;
mHeight = size.y;
mDesntiy = metrics.densityDpi;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SCREENSHOT_INTENT_IDENTIFIER) {
if (resultCode == RESULT_OK) {
mMediaProjection = mMediaProjectManager.getMediaProjection(resultCode, data);
}
}
}
void scheduleCapture() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
captureScreen();
}
}, 2000);
}
private void captureScreen() {
Log.d(TAG, "Take screen shot");
Log.d(TAG, "thread > "+Thread.currentThread().getName());
Log.d(TAG, "current thread > "+(Looper.myLooper() == Looper.getMainLooper()));
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
Surface surface = new Surface(new SurfaceTexture(5555));
mMediaProjection.createVirtualDisplay("screenshot", mWidth, mHeight, mDesntiy,
VIRTUAL_DISPLAY_FLAGS,mImageReader.getSurface()
, null, mHandler);
mImageReader.setOnImageAvailableListener(this, mHandler);
scheduleCapture();
}
@Override
public void onImageAvailable(ImageReader imageReader) {
Log.d(TAG, "onImageAvailable ");
Log.d(TAG, "thread > "+Thread.currentThread().getName());
Log.d(TAG, "current thread > "+(Looper.myLooper() == Looper.getMainLooper()));
Image image = imageReader.acquireLatestImage();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * metrics.widthPixels;
// create bitmap
Bitmap bmp = Bitmap.createBitmap(metrics.widthPixels + (int) ((float) rowPadding /
(float) pixelStride), metrics.heightPixels, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buffer);
image.close();
imageReader.close();
Bitmap realSizeBitmap = Bitmap.createBitmap(bmp, 0, 0, metrics.widthPixels, bmp.getHeight
());
bmp.recycle();
saveScreenShot(realSizeBitmap);
}