How can I take a screenshot of a selected area of phone-screen not by any program but from code?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
ADD PERMISSION
Most of the answers for this question use the the
Canvas
drawing method or drawing cache method. However, theView.setDrawingCache()
method is deprecated in API 28. Currently the recommended API for making screenshots is thePixelCopy
class available from API 24 (but the methods which acceptWindow
parameter are available from API 26 == Android 8.0 Oreo). Here is a sample Kotlin code for retrieving aBitmap
:Add the permission in the manifest
For Supporting Marshmallow or above versions, please add the below code in the activity onCreate method
As a reference, one way to capture the screen (and not just your app activity) is to capture the framebuffer (device /dev/graphics/fb0). To do this you must either have root privileges or your app must be an app with signature permissions ("A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission") - which is very unlikely unless you compiled your own ROM.
Each framebuffer capture, from a couple of devices I have tested, contained exactly one screenshot. People have reported it to contain more, I guess it depends on the frame/display size.
I tried to read the framebuffer continuously but it seems to return for a fixed amount of bytes read. In my case that is (3 410 432) bytes, which is enough to store a display frame of 854*480 RGBA (3 279 360 bytes). Yes, the frame, in binary, outputted from fb0 is RGBA in my device. This will most likely depend from device to device. This will be important for you to decode it =)
In my device /dev/graphics/fb0 permissions are so that only root and users from group graphics can read the fb0.
graphics is a restricted group so you will probably only access fb0 with a rooted phone using su command.
Android apps have the user id (uid) = app_## and group id (guid) = app_## .
adb shell has uid = shell and guid = shell, which has much more permissions than an app. You can actually check those permissions at /system/permissions/platform.xml
This means you will be able to read fb0 in the adb shell without root but you will not read it within the app without root.
Also, giving READ_FRAME_BUFFER and/or ACCESS_SURFACE_FLINGER permissions on AndroidManifest.xml will do nothing for a regular app because these will only work for 'signature' apps.
Also check this closed thread for more details.
Take screenshot of a view in android.
For those who want to capture a GLSurfaceView, the getDrawingCache or drawing to canvas method won't work.
You have to read the content of the OpenGL framebuffer after the frame has been rendered. There is a good answer here