为什么添加清单 CAPTURE_VIDEO_OUTPUT 或 CAPTURE_SECURE_VIDEO_OUTPUT 时会出错?

Why error occurs when adding a manifest CAPTURE_VIDEO_OUTPUT or CAPTURE_SECURE_VIDEO_OUTPUT?

遇到这样的问题。随着这段代码的执行,出现异常:

DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); int mDensity = displayMetrics.densityDpi;

        DisplayManager dM = (DisplayManager) getSystemService(ContextWrapper.DISPLAY_SERVICE);

        ImageReader imR = ImageReader.newInstance(300, 300, PixelFormat.RGBA_8888, 2);


        VirtualDisplay vD = dM.createVirtualDisplay("VladScreen", 300, 300, mDensity, imR.getSurface(), DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC);

异常情况如下:

 java.lang.SecurityException: Requires CAPTURE_VIDEO_OUTPUT or CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate MediaProjection token in order to create a screen sharing virtual display.
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.os.Parcel.readException(Parcel.java:1599)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.os.Parcel.readException(Parcel.java:1552)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.hardware.display.IDisplayManager$Stub$Proxy.createVirtualDisplay(IDisplayManager.java:484)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.hardware.display.DisplayManagerGlobal.createVirtualDisplay(DisplayManagerGlobal.java:377)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.hardware.display.DisplayManager.createVirtualDisplay(DisplayManager.java:537)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.hardware.display.DisplayManager.createVirtualDisplay(DisplayManager.java:529)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.hardware.display.DisplayManager.createVirtualDisplay(DisplayManager.java:480)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at com.example.novoseltsev.applicationpopupwindow.MainActivity.onCreate(MainActivity.java:106)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.Activity.performCreate(Activity.java:6237)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-27 04:03:30.321 7739-7739/com.example.novoseltsev.applicationpopupwindow W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-27 04:03:30.363 7739-11879/com.example.novoseltsev.applicationpopupwindow D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

我知道它没有添加到权限 CAPTURE_VIDEO_OUTPUTCAPTURE_SECURE_VIDEO_OUTPUT

但添加 manifest.xml 异常后仍然存在

<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT"/>

您使用哪个 android 版本?如果是 Android 6.0 Marshmallow,则需要请求许可。 Preference here

为系统服务,不为第三方应用服务。

https://developer.android.com/reference/android/hardware/display/DisplayManager.html#VIRTUAL_DISPLAY_FLAG_PUBLIC

创建不限于 own-content 的 public 虚拟显示只会隐式创建 auto-mirroring 显示。有关允许创建 auto-mirroring 显示的限制,请参阅 VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR。

https://developer.android.com/reference/android/hardware/display/DisplayManager.html#VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR …… 创建 auto-mirroing 虚拟显示需要 CAPTURE_VIDEO_OUTPUT 或 CAPTURE_SECURE_VIDEO_OUTPUT 权限。这些权限保留供系统组件使用,不适用于 third-party 应用程序。或者,可以使用适当的 MediaProjection 来创建 auto-mirroring 虚拟显示。