有些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,那么该有的信息已经全有了,就此可以操作很多东西了。
谢谢。