Android VirtualDisplay.release() 不释放显示 [英] Android VirtualDisplay.release() not releasing the display
问题描述
我正在使用 android MediaProjection
截取屏幕截图.截屏后需要停止投影,虚拟显示
应该被释放,但VirtualDisplay.release()
没有释放显示.这是创建显示的代码.
I'm using android MediaProjection
for taking screenshot. The projection needs to be stopped after taking screenshot and virtual display
should be released but VirtualDisplay.release()
is not releasing the display. Here is the code to create display.
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
实现.
To stop the projection, I call sMediaProjection.stop();
and here is my MediaProjectionStopCallback
implementation.
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();
}
}
这是日志猫.
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
中释放显示后,当我检查可用显示时,虚拟显示仍列为可用显示.它会影响手机显示和图形,我无法播放任何视频,即使卸载应用程序后问题仍然存在,直到我重新启动手机.
我已经检查了此链接 Android 虚拟显示版本不会删除显示 并搜索了很多但没有找到有用的.非常感谢您的帮助.
I have double checked, the above onStop
method is properly called as can be seen in the logcat. After releasing the display in onStop
when I check the available displays, the virtual display is still listed as an available display. 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.
I have checked this link Android virtual display release does not remove display and searched a lot but nothing found helpful. Your help is highly appreciated.
推荐答案
试试这个?
if (mOrientationChangeCallback != null) {
mOrientationChangeCallback.disable();
}
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
}
if (mMediaProjection != null) {
mMediaProjection.stop();
}
if (mImageReader != null) {
mImageReader.setOnImageAvailableListener(null, null);
mImageReader.close();
}
这篇关于Android VirtualDisplay.release() 不释放显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!