Nathan
Nathan

Reputation: 1

C# MAUI Android - Take screenshot of other apps using accessibility services

I am developing an application for Android using .NET MAUI C#. I have created an accessibility service my goal is to take screenshots of the screen. The code below takes only screenshots of the app itself.

Screenshot.Default.CaptureAsync();

I have implemented the code below in C# however I can't figure out how to implement ITakeScreenshotCallback I have created a class that implement the interface but did not work I am getting an invalid cast exception, I have no more information about the error.

Link: Code in Java I am trying to implement

Method to take screenshot C#:

 public override void TakeScreenshot(int displayId, IExecutor executor, ITakeScreenshotCallback callback)
 {
     base.TakeScreenshot(displayId, executor, callback);
 }

Java code I'm trying to convert to C#, I can't figure out how to implement TakeScreenshotCallback:

 takeScreenshot(Display.DEFAULT_DISPLAY, getApplicationContext().getMainExecutor(), new TakeScreenshotCallback() {
            @Override
            public void onSuccess(@NonNull ScreenshotResult screenshotResult) {
                Log.d("SCREENSHOT", "Success");
                sudokuMap = Bitmap.wrapHardwareBuffer(screenshotResult.getHardwareBuffer(), screenshotResult.getColorSpace());
                sudokuMap = Bitmap.createBitmap(sudokuMap, 39,498,1002,1002);
                result = true;
                Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_LONG).show();
                saveImage(sudokuMap);
            }

            @Override
            public void onFailure(int i) {
                Log.d("SCREENSHOT", "Failed " + i);
                sudokuMap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
                result = false;
                Toast.makeText(getBaseContext(), "Failed " + i, Toast.LENGTH_LONG).show();
            }
        });

Any help would be greatly appreciated.

Thank you

Upvotes: 0

Views: 190

Answers (0)

Related Questions