MediaProjectionManager.getMediaProjection( ) problem in android 12

671 Views Asked by At

I create a screenshot application and that work good in all of the android devices. But I have a problem with take screenshot in android 12 (Xiaomi 11t) I'm using static intent and result code and then clone intent and pass it to this function mediaProjectionManager.getMediaProjection(resultCode,clonedIntent) I have no problem with take screen shot for first time but I can't take screen shot for second time and I get this error. if you need more info please tell me.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.koala.classor, PID: 10824
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.Set.add(java.lang.Object)' on a null object reference
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2431)
    at android.os.Parcel.createException(Parcel.java:2409)
    at android.os.Parcel.readException(Parcel.java:2392)
    at android.os.Parcel.readException(Parcel.java:2334)
    at android.media.projection.IMediaProjection$Stub$Proxy.start(IMediaProjection.java:235)
    at android.media.projection.MediaProjection.<init>(MediaProjection.java:59)
    at android.media.projection.MediaProjectionManager.getMediaProjection(MediaProjectionManager.java:119)
    at com.koala.classor.G.getMediaProjection(G.java:86)
    at com.koala.classor.OverScreenWindowService$2$1.run(OverScreenWindowService.java:194)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8105)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
 Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.media.projection.MediaProjectionManagerService$MediaProjection.start(MediaProjectionManagerService.java:553)
    at android.media.projection.IMediaProjection$Stub.onTransact(IMediaProjection.java:137)
    at android.os.Binder.execTransactInternal(Binder.java:1182)
    at android.os.Binder.execTransact(Binder.java:1146)

 
1

There are 1 best solutions below

1
On

MIUI13+Android12

The RemoteException error is reported on line 553 of MediaProjectionManagerService. After checking the source code, the error message is that the package for applying for mediaprojection cannot be found. It is guessed that MIUI maintains a separate list for this, only the package that has applied for permission will be in it, and it will be removed after obtaining and closing mediaprojection. No such mechanism has been seen in other systems.

In other words, intent data cannot be reused. The solution is also very simple, just don’t close the mediaprojection after using it.