Search code examples
javaandroidscreenshot

Android Accessbility Service TakeScreenshot get exception: java.lang.SecurityException: Services don't have the capability of taking the screenshot


The following code running in AccessbilityService got exception. Android API Level 30 (Android 11)

It didn't hit the callback nor "onSuccess" nor "onFailure". Just throw exception when calling the takeScreenshot function.

Did I miss any permissions? Any ideas please? Thanks a lot~

Exception:

2022-01-29 13:38:53.026 26433-26433/com.example.st E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spinstar, PID: 26433
java.lang.SecurityException: Services don't have the capability of taking the screenshot.
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
    at android.os.Parcel.createException(Parcel.java:2360)
    at android.os.Parcel.readException(Parcel.java:2343)
    at android.os.Parcel.readException(Parcel.java:2285)
    at android.accessibilityservice.IAccessibilityServiceConnection$Stub$Proxy.takeScreenshot(IAccessibilityServiceConnection.java:1564)
    at android.accessibilityservice.AccessibilityService.takeScreenshot(AccessibilityService.java:2042)

Solution

  • You should add

    android:canTakeScreenshot="true"
    

    to your

    accessibility_service_config.xml
    

    in Android 11+ (API level 30+). Read more over here