Android: wrong image size from mediaProjection on foldable device

44 Views Asked by At

MediaProjectionFoldable

There is some problem with mediaProjection on foldable devices:

Steps to reproduce

  1. Get current width and height in Activity:
   val wmc = WindowMetricsCalculator.getOrCreate()
   val bounds = wmc.computeCurrentWindowMetrics(this).bounds
  1. Run Service for mediaProjection:
   val wmc = WindowMetricsCalculator.getOrCreate()
   val bounds = wmc.computeCurrentWindowMetrics(this).bounds

   val intent = Intent(this, ScreenCaptureService::class.java)
   intent.putExtra("RESULT_CODE", resultCode)
   intent.putExtra("DATA", data)
   intent.putExtra("W", bounds.width())
   intent.putExtra("H", bounds.height())
   startService(intent)

  1. Create ScreenCaptureService with mediaProjection:
   val mediaProjectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager

        width = intent.getIntExtra("W", 0)
        height = intent.getIntExtra("H", 0)

        imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2)
        imageReader!!.setOnImageAvailableListener({
            // save iamge!
        }, mHandler)

        mediaProjection = mediaProjectionManager.getMediaProjection(...)


        virtualDisplay = mediaProjection!!.createVirtualDisplay(
            "ScreenCapture",
            width,
            height,
            Resources.getSystem().displayMetrics.densityDpi,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY or DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
            imageReader!!.surface,
            null, null
        )
  1. Run it on foldable device. What we can see:

It looks like it is image has wrong size and offset. By the way: bounds in Activity is:

Rect(0, 0 - 1148, 2480)

So, left offset is 0.

There is demo application with mediaprojection and foldable API :https://github.com/RPetrov/MediaProjectionFoldable

How can I fix it? Is there some API to use mediaProjection on foldable devices?

0

There are 0 best solutions below