android 截屏内容的获取

有些app的开发需求中或许会包含 得到截屏的图片 截屏的通知等情况。

那么来讲一下如何获取。当然截屏的快捷键自己网络查询。



主要用到的类为ContentObserver,下面来看看详细的代码:

 private static final String EXTERNAL_CONTENT_URI_MATCHER =
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
private static final String[] PROJECTION = new String[] {
        MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA,
        MediaStore.Images.Media.DATE_ADDED
};
private static final String SORT_ORDER = MediaStore.Images.Media.DATE_ADDED + " DESC";
private static final long DEFAULT_DETECT_WINDOW_SECONDS = 10;
final ContentObserver contentObserver = new ContentObserver(null) {
                    @Override
                    public void onChange(boolean selfChange, Uri uri) {
                        if (uri.toString().matches(EXTERNAL_CONTENT_URI_MATCHER)) {
                            Cursor cursor = null;
                            try {
                                cursor = contentResolver.query(uri, PROJECTION, null, null,
                                        SORT_ORDER);
                                if (cursor != null && cursor.moveToFirst()) {
                                    String path = cursor.getString(
                                            cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                                    long dateAdded = cursor.getLong(cursor.getColumnIndex(
                                            MediaStore.Images.Media.DATE_ADDED));
                                    long currentTime = System.currentTimeMillis() / 1000;
                                    Log.d(TAG, "path: " + path + ", dateAdded: " + dateAdded +
                                            ", currentTime: " + currentTime);
                                    if (path.toLowerCase().contains("screenshot") &&
                                            Math.abs(currentTime - dateAdded) <=
                                                    DEFAULT_DETECT_WINDOW_SECONDS) {
                                        subscriber.onNext(path);
                                    }
                                }
                            } catch (Exception e) {
                                Log.d(TAG, "open cursor fail");
                            } finally {
                                if (cursor != null) {
                                    cursor.close();
                                }
                            }
                        }
                        super.onChange(selfChange, uri);
                    }
                };
                contentResolver.registerContentObserver(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, contentObserver);
以上就是获取截屏的主要信息,主要是通过ContentObserver的内容监听,然后获取Cursor,那么该有的信息已经全有了,就此可以操作很多东西了。



谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android获取屏幕显示内容有两种方式: 1. 使用MediaProjection API 使用MediaProjection API可以截屏并将屏幕上的内容转换为视频流,然后可以对视频流进行处理。以下是获取屏幕截图的代码示例: ``` private MediaProjectionManager mMediaProjectionManager; private MediaProjection mMediaProjection; private ImageReader mImageReader; private void startScreenCapture() { mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE); Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent(); startActivityForResult(captureIntent, REQUEST_CODE); mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2); mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, resultData); mMediaProjection.createVirtualDisplay("ScreenCapture", width, height, densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); } private void stopScreenCapture() { if (mMediaProjection != null) { mMediaProjection.stop(); mMediaProjection = null; } if (mImageReader != null) { mImageReader.close(); mImageReader = null; } } ``` 2. 使用AccessibilityService 使用AccessibilityService可以获取屏幕上的所有视图,并对其进行操作。以下是获取屏幕上所有TextView的代码示例: ``` public class MyAccessibilityService extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) { AccessibilityNodeInfo rootNode = getRootInActiveWindow(); if (rootNode != null) { List<AccessibilityNodeInfo> textViews = rootNode.findAccessibilityNodeInfosByViewId("android:id/text1"); for (AccessibilityNodeInfo textView : textViews) { Log.d(TAG, "TextView text: " + textView.getText()); } } } } @Override public void onInterrupt() {} @Override protected void onServiceConnected() { super.onServiceConnected(); AccessibilityServiceInfo config = new AccessibilityServiceInfo(); config.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED; config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC; config.flags = AccessibilityServiceInfo.DEFAULT; setServiceInfo(config); } } ``` 需要注意的是,使用AccessibilityService获取屏幕内容需要用户授权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值