Reputation: 71
Problem Statement: Media projection permission dialog continuously appears on Samsung Android 14 One UI 6 and Pixel 6 devices, disrupting normal functionality. This issue does not occur on other devices.
Request: Seeking guidance or solutions to resolve the repeated appearance of the media projection permission dialog on the mentioned devices. Any insights, troubleshooting steps, or potential fixes would be greatly appreciated.
I have used this code.Pls check
private fun setupMediaIntent() {
try {
if (!screenshotUtil.isMediaProjectionEnabled()) {
val mediaProjectionManager =
context?.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
startForResult.launch(mediaProjectionManager.createScreenCaptureIntent())
} else {
handleNavigation()
}
} catch (exx: Exception) {
exx.printStackTrace()
}
}
private val startForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
try {
if (activityResult.resultCode == Activity.RESULT_OK) {
screenshotUtil.setResultCode(activityResult.resultCode)
screenshotUtil.setMediaProjectionIntent(activityResult.data)
ServiceUtils.restartService(
requireContext(),
ServiceType.AUTO_SCREENSHOTS.value
)
handleNavigation()
} else {
setupMediaIntent()
}
} catch (exx: Exception) {
exx.printStackTrace()
}
}
private fun handleNavigation() {
PreferencesUtils.setPreference(requireContext(), SETUP_FINISHED, "true")
PreferencesUtils.setRestartVirtualDisplay(requireContext(), "false")
if (initialSetup || fromDashboard) {
LogUtil.writeLog(
requireContext(),
"Initial Permission",
"Media Projection Permission Enabled"
)
findNavController().popBackStack(R.id.mainFragment, false)
} else {
activity?.finishAffinity()
}
Constants.finishes = true
}
Excepting Behavior : the permission dialog should not popup again & again one it enabled
Upvotes: 0
Views: 695
Reputation: 26
Also, make sure you don't shut down the surface at any point on android 14+ before you are completely done with media projection, had that issue myself. Couldn't find why my service didn't work, then discovered I had shut down the surface immediately after capturing a valid frame. By leaving the surface, image reader, and virtual display active, you'll be able to just use something like this repeatedly. Beware that this approach is constantly consuming resources as well!
val image = imageReader.acquireNextImage()
if (image != null) {
val planes = image.planes
val buffer = planes[0].buffer
val pixelStride = planes[0].pixelStride
val rowStride = planes[0].rowStride
val rowPadding = rowStride - pixelStride * width
val bitmapWidth = width + rowPadding / pixelStride
if (latestBitmap == null || latestBitmap!!.width != bitmapWidth || latestBitmap!!.height != height) {
latestBitmap?.recycle()
latestBitmap = Bitmap.createBitmap(bitmapWidth, height, Bitmap.Config.ARGB_8888)
}
latestBitmap?.copyPixelsFromBuffer(buffer)
image.close()
Upvotes: 0
Reputation: 36
Don't release MediaProjection, VirtualDisplay, ImageReader, repeat Use mImageReader.acquireLatestImage().
Upvotes: 1
Reputation: 2706
if "isMediaProjectionEnabled()" returns TRUE you have to provide same "activityResult.data" as before, or the Dialog will be shown. It's a strange behaviour but that's it.
Upvotes: 0