有人知道它是什么吗?已放弃BufferQueue
我正在使用Mediaprojectoin类拍摄屏幕截图。我的应用程序运行良好,但在Logcat中,我有很多行写着"BufferQueue已被放弃"。我不知道它是什么,我应该做什么。
事实上,我在android 5中没有这个问题,只是针对android 6+。
这是我的代码:
Display display = wm.getDefaultDisplay();
// final DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Point size = new Point();
display.getRealSize(size);
final int mWidth = size.x;
final int mHeight = size.y;
int mDensity = metrics.densityDpi;
ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
mProjection.createVirtualDisplay("screen-mirror", mWidth, mHeight, mDensity, flags, mImageReader.getSurface(), null, handler);
mImageReader.setOnImageAvailableListener(new mImageListener(handler,metrics,mImageReader),handler);
这是ImageListener:
class mImageListener implements ImageReader.OnImageAvailableListener{
private Handler handler ;
private DisplayMetrics metrics ;
ImageReader imageReader ;
public mImageListener(Handler handler ,DisplayMetrics metrics , ImageReader imageReader ){
this.handler = handler ;
this.metrics = metrics ;
this.imageReader = imageReader ;
}
@Override
public void onImageAvailable(ImageReader reader) {
reader.setOnImageAvailableListener(null, handler);
Image image = reader.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();
reader.close();
Bitmap realSizeBitmap = Bitmap.createBitmap(bmp, 0, 0, metrics.widthPixels, bmp.getHeight());
bmp.recycle();
FileOutputStream fos = null;
Bitmap bitmap = realSizeBitmap;
try {
File img1 = new File("/sdcard/DCIM"+"/ins.jpg" ) ;
fos = new FileOutputStream(img1) ;
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos) ;
Toast.makeText(getApplicationContext() ,"Screen was taken" ,Toast.LENGTH_LONG ).show();
fos.flush();
fos.close();
imageReader = null ;
}
catch (Exception e){
Toast.makeText(getApplicationContext() , "exception happend + " + e.toString(),Toast.LENGTH_LONG).show() ;
}
}
}
你能帮我吗。。。
我曾经遇到过这个问题。你需要释放并停止媒体投影。由于异步ImageReader请求,可能会将此异常抛出到logcat中。
解决方案:
步骤1:制作一个名为releaseAll((的函数
private void releaseAll(){
display.release();
mImageReader.getSurface().release();
mImageReader.close()
if (mProjection != null) mProjection.stop()
mMediaProjection = null
mImageReader = null
}
步骤2:
将image.close((之前的上述方法调用到onImageAvailable 中
releaseAll();
image.close();