MediaProjection.CreateVirtualDisplay Method

Definition

Creates a VirtualDisplay to capture the contents of the screen.

[Android.Runtime.Register("createVirtualDisplay", "(Ljava/lang/String;IIIILandroid/view/Surface;Landroid/hardware/display/VirtualDisplay$Callback;Landroid/os/Handler;)Landroid/hardware/display/VirtualDisplay;", "")]
public Android.Hardware.Display.VirtualDisplay? CreateVirtualDisplay(string name, int width, int height, int dpi, Android.Views.DisplayFlags flags, Android.Views.Surface? surface, Android.Hardware.Display.VirtualDisplay.Callback? callback, Android.OS.Handler? handler);
[<Android.Runtime.Register("createVirtualDisplay", "(Ljava/lang/String;IIIILandroid/view/Surface;Landroid/hardware/display/VirtualDisplay$Callback;Landroid/os/Handler;)Landroid/hardware/display/VirtualDisplay;", "")>]
member this.CreateVirtualDisplay : string * int * int * int * Android.Views.DisplayFlags * Android.Views.Surface * Android.Hardware.Display.VirtualDisplay.Callback * Android.OS.Handler -> Android.Hardware.Display.VirtualDisplay

Parameters

name
String

The name of the virtual display, must be non-empty.

width
Int32

The width of the virtual display in pixels. Must be greater than 0.

height
Int32

The height of the virtual display in pixels. Must be greater than 0.

dpi
Int32

The density of the virtual display in dpi. Must be greater than 0.

flags
DisplayFlags

A combination of virtual display flags. See DisplayManager for the full list of flags.

surface
Surface

The surface to which the content of the virtual display should be rendered, or null if there is none initially.

callback
VirtualDisplay.Callback

Callback to call when the virtual display's state changes, or null if none.

handler
Handler

The Handler on which the callback should be invoked, or null if the callback should be invoked on the calling thread's main Looper.

Returns

The created virtual display, or null if no virtual display could be created.

Attributes

Exceptions

For applications targeting Android 14 (API level 34) or later, if no MediaProjection.Callback is registered.

If the projection has been stopped; or, for applications targeting Android 14 (API level 34) or later, if its OnStop() callback has been received, if this projection has already started a recording, or if it was obtained by invoking GetMediaProjection(Int32, Intent) more than once for the same consent result.

Remarks

Register a MediaProjection.Callback before calling this method. Override OnStop() to release the virtual display, surface, and related resources and to update any UI that represents the projection state.

For applications targeting Android 14 (API level 34) or later, each user consent session grants one call to this method. Do not reuse the result data passed to GetMediaProjection(Int32, Intent) to obtain multiple projection instances, and do not call this method more than once on the same projection instance.

After Stop() is called, this projection can no longer create a virtual display. For applications targeting Android 14 (API level 34) or later, the same restriction applies after OnStop() is received. For applications targeting an earlier version, the user can be prompted to grant consent again instead of an exception being thrown.

Java documentation for android.media.projection.MediaProjection.createVirtualDisplay(java.lang.String, int, int, int, int, android.view.Surface, android.hardware.display.VirtualDisplay.Callback, android.os.Handler).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also