Search code examples
kotlinbackgroundscreenshotandroid-mediaprojectionmediaprojectionmanager

How can i take screenshot in background after a min and save it in gallery not of my but my phone?


Hello Stack Overflow community,

I am working on an Android app where I need to capture background screenshots periodically. I've explored two approaches: using MediaProjection and using a Canvas. However, I'm facing challenges with the MediaProjection method, specifically, the mediaProjection object appears to be null, rendering the approach ineffective.

Here is a brief overview of what I have tried:

MediaProjection Method:

I am using the MediaProjection API to capture screenshots in the background. The mediaProjection object obtained from mediaProjectionManager.getMediaProjection is consistently null. Minimum Android version tested: API 22, Maximum: Latest. Canvas Method:

I also attempted capturing screenshots using a Canvas. However, I encountered limitations in its implementation and it didn't fully meet my requirements. General Details:

The goal is to capture screenshots in the background, not just within the app. The app should run as a service or worker and take screenshots at predefined intervals. I have thoroughly reviewed my code and tried various solutions, but the issue persists. I'm seeking guidance on how to achieve background screenshots successfully, considering the limitations I've encountered


Solution

  • Have you setup a foreground service to manage the screenshot handling? You'll need to ask user permission every time the app runs as well or the result will be ull from the mediaProjection will be null. Also once you have the mediaProjection instance, instaed of using it to get the screenshot, you may want to consider cloning the intent data and using the clone if the device is running android 13> to save resources. Lots of good answers and advice on here that have helped me a lot Using mediaprojectionmanager to get screenshots while another app is in the foreground