在Android 11执行如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/** * 保存拍照的图片是否已经完成 * * @param context Context 对象 * @return 查询成功返回 true ,否则返回false */ private static boolean isSavePictureComplete(@NonNull final Context context, long takeTime) { //扫描图片 final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final ContentResolver resolver = context.getContentResolver(); final Cursor cursor = resolver.query(uri, new String[]{ MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media._ID, MediaStore.Images.Media.SIZE }, MediaStore.MediaColumns.SIZE + ">0", null, MediaStore.Files.FileColumns._ID + " DESC limit 1 offset 0"); //读取扫描到的图片 if ((null != cursor) && (cursor.getCount() > 0) && cursor.moveToFirst()) { //获取图片时间 long time = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID)); if (String.valueOf(time).length() < 13) { time *= 1000; } cursor.close(); // 如果照片的插入时间大于相机的拍照时间,就认为是拍照图片已插入 return time + 1000 < takeTime; } return true; } |
会出现如下错误:
1 2 3 4 5 6 7 |
java.lang.IllegalArgumentException: Invalid token limit at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142) at android.content.ContentProviderProxy.query(ContentProviderNative.java:472) at android.content.ContentResolver.query(ContentResolver.java:1197) at android.content.ContentResolver.query(ContentResolver.java:1128) at android.content.ContentResolver.query(ContentResolver.java:1084) |
原因为 Android 11 上使用 ContentResolver 的时候,已经不允许进行 SQL语句拼接,主要是为了防止 SQL注入攻击。
代码参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/** * 保存拍照的图片是否已经完成 * * @param context Context 对象 * @return 查询成功返回 true ,否则返回false */ private static boolean isSavePictureComplete(@NonNull final Context context, long takeTime) { final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final String[] columns = new String[]{ MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media._ID, MediaStore.Images.Media.SIZE}; //扫描图片 final ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q/*29*/) { final Bundle bundle = new Bundle(); bundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, MediaStore.MediaColumns.SIZE + " > ?"); bundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, new String[]{"0"}); bundle.putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, new String[]{MediaStore.Files.FileColumns._ID}); bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING); bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, 1); bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, 0); cursor = contentResolver.query(uri, columns, bundle, null); } else { cursor = contentResolver.query(uri, columns, MediaStore.MediaColumns.SIZE + ">0", null, MediaStore.Files.FileColumns._ID + " DESC limit 1 offset 0"); } //读取扫描到的图片 if ((null != cursor) && (cursor.getCount() > 0) && cursor.moveToFirst()) { //获取图片时间 long time = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); if (String.valueOf(time).length() < 13) { time *= 1000; } // 如果照片的插入时间大于相机的拍照时间,就认为是拍照图片已插入 return time + 1000 < takeTime; } } finally { if (null != cursor) { cursor.close(); } } return true; } |
参考链接
limiting number of rows in a ContentResolver.query() function