Method for programming screen capture function for Android 5.0 and above
This paper describes the method of programming screen capture function for Android 5.0 and above. Share with you for your reference, as follows:
Before Android 5.0 and API 21, if you want to take a screenshot, the system screen must be completed by root. After 5.0, the interface is opened. Let's see how we implement it.
1、 Related classes involved
1. MediaProjectionManager
Official words: manages the retrieval of certain types of {@ link mediaprojection} tokens. This class uses media in context #getsystemservice_ PROJECTION_ The function of service is to obtain mediaprojection
2. MediaProjection
A token granting applications the ability to capture screen contents and / or record system audio. The exact capabilities granted dependent on the type of MediaProject
3. ImageReader
The imagereader class allows direct application access to image data rendered into a {@ link Android. View. Surface}
2、 The above three classes can complete the operation of capturing screen images. Next, we will explain how they cooperate
1. First obtain the user's authorization. The screenshot screen can be operated only after the user's manual authorization
You must use startactivityforresult here, because the result of the screen capture authorized by the user will be returned in the createscreencaptureintent() method, and the user will allow or reject it according to the following pop-up window
The result data returned by the operation in onactivityresult of activity after user selection
Here, I use the float windows service to display a hover button on the desktop and click the screenshot. Let's see how to realize the screenshot in the float windows service
2. Intercept screen content to generate bitmap
First create an imagereader instance
Then click on the event to trigger startscreenshot ()
One thing we do in the startvirtual () method is to get the current screen content
At the same time, you need to obtain the mediaprojection instance, and mresultdata is the result returned after authorization
Finally get the content of the current screen. Note that mimagereader. Getsurface() is passed in here, and the screen data will also be in the surface in imagereader
Finally, the screen content data obtained by mimagereader is converted into pictures and processed in asynctask,
The buffer data in image.plane is not completely required by bitmap. Note the following three points
1. The image format set by image must be consistent with that set by bitmap
2. There are row gaps in buffered data, so we must remove these gaps
3. Image. Close() must be called after image is used; Close it, otherwise an error will be reported if it is used again
Finally, save the generated bitmap, and it's OK
GitHub source code download address: https://github.com/goodbranch/ScreenCapture
Apk file download address: https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/screenshot/ScreenCapture.apk
More readers interested in Android related content can view the special topics of this site: summary of Android graphics and image processing skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage Summary of Android view skills, summary of Android layout skills and summary of Android control usage
I hope this article will help you in Android programming.