How can Android retrieve the content displayed on the screen?
To capture the display content of an Android device, you can utilize the MediaProjection API provided by Android. The following is a simple example code:
- Firstly, add the following permissions in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- Create a new Activity and add the following code inside its onCreate method.
private static final int REQUEST_CODE_MEDIA_PROJECTION = 1;
private MediaProjectionManager mMediaProjectionManager;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private ImageReader mImageReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE_MEDIA_PROJECTION);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_MEDIA_PROJECTION) {
if (resultCode == RESULT_OK) {
mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
// 获取屏幕的宽度和高度
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
// 创建一个ImageReader对象,用于捕捉屏幕内容
mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
width, height, metrics.densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
}
}
}
- Add the following code where screen display content needs to be accessed:
Image image = mImageReader.acquireLatestImage();
if (image != null) {
// 处理屏幕内容
// ...
// 释放Image资源
image.close();
}
Please note that the use of the MediaProjection API requires user authorization, so it is necessary to call the startActivityForResult method in the Activity to obtain the user’s authorization. Upon successful authorization, the screen content can be accessed through the ImageReader object.