我正在使用android MediaProjection
来截取屏幕截图。在截屏后需要停止投影,virtual display
应该被释放,但VirtualDisplay.release()
没有释放显示。这是创建显示的代码。
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
// display metrics
DisplayMetrics metrics = getResources().getDisplayMetrics();
mDensity = metrics.densityDpi;
mDisplay = getWindowManager().getDefaultDisplay();
Point size = new Point();
mDisplay.getSize(size);
mWidth = size.x;
mHeight = size.y;
// register media projection stop callback
sMediaProjection.registerCallback(new MediaProjectionStopCallback(), mHandler);
// register orientation change callback
mOrientationChangeCallback = new OrientationChangeCallback(this);
if (mOrientationChangeCallback.canDetectOrientation()) {
mOrientationChangeCallback.enable();
}
// start capture reader
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 1);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
}
}
要停止投影,我致电sMediaProjection.stop();
,这是MediaProjectionStopCallback
实施。
private class MediaProjectionStopCallback extends MediaProjection.Callback {
@Override
public void onStop() {
Log.e("ScreenCapture", "stopping projection.");
mHandler.post(new Runnable() {
@Override
public void run() {
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
Log.e("Virtual Display", "Released");
}
if (mImageReader != null)
mImageReader.setOnImageAvailableListener(null, null);
if (mOrientationChangeCallback != null)
mOrientationChangeCallback.disable();
sMediaProjection.unregisterCallback(MediaProjectionStopCallback.this);
DisplayManager disp = (DisplayManager) getSystemService(DISPLAY_SERVICE);
Display[] allDisplays = disp.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
Log.e(TAG + "-Display", "Display Count " + allDisplays.length);
for (Display dl : allDisplays) {
Log.e(TAG + "-Display", "Display name: " + dl.getName() + " Display id: " + dl.getDisplayId());
}
}
});
//Toast.makeText(getApplicationContext(), "Projection Stopped", Toast.LENGTH_SHORT).show();
}
}
这是logcat。
03-02 14:52:55.925 8264-8732/codistan.pk.squeeze_me E/ScreenCapture﹕ stopping projection.
03-02 14:52:55.925 8264-8732/codistan.pk.squeeze_me E/Virtual Display﹕ Released
03-02 14:52:55.925 8264-8732/codistan.pk.squeeze_me E/codistan.pk.squeeze_me.ScreenCaptureActivity-Display﹕ Display Count 1
03-02 14:52:55.935 8264-8732/codistan.pk.squeeze_me E/codistan.pk.squeeze_me.ScreenCaptureActivity-Display﹕ Display name: screencap Display id: 1
我已经仔细检查过,上面的onStop
方法被正确调用,可以在logcat中看到。当我检查可用显示时,在onStop
中释放显示后,虚拟显示仍然列为可用显示。 It affects the phone display and graphics and I can't play any video and the issue remains even after uninstalling the app untill I restart the phone.
我已查看此链接Android virtual display release does not remove display并进行了大量搜索但未发现任何有用的信息。非常感谢您的帮助。
答案 0 :(得分:0)
试试这个?
2017-12-07T12:05:45Z