簡體   English   中英

在 Android Q 上查詢 MediaStore

[英]Query MediaStore on Android Q

我正在使用 MediaStore 獲取所有圖像專輯,並且在 Android Q 之前運行良好。但是今天我將編譯版本增加到 29,它拋出了這個異常:

android.database.sqlite.SQLiteException: near "GROUP": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT bucket_id, bucket_display_name, mime_type FROM images WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND ( GROUP BY bucket_id )

這是我查詢 MediaStore 的代碼:

    String[] PROJECTION_BUCKET = {
            MediaStore.Images.ImageColumns.BUCKET_ID,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.MIME_TYPE};

    String BUCKET_GROUP_BY = "1) GROUP BY 1,(2";
    String BUCKET_ORDER_BY = "MAX(datetaken) DESC";

    Cursor cur = context.getContentResolver().query(getUriOfType(type),
            PROJECTION_BUCKET,
            BUCKET_GROUP_BY,
            null,
            BUCKET_ORDER_BY);

由於組不在Android Q工作。

有什么解決辦法嗎?

GROUP BY不再適用於 android 10(Q) 並且COUNT也不再適用。 以下代碼片段可能對您有所幫助。

String path = null;
String album = null;
String timestamp = null;
String countPhoto = null;
Uri uriExternal = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri uriInternal = android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
String[] projection = {
        MediaStore.MediaColumns.DATA,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.MediaColumns.DATE_MODIFIED
};
Cursor cursorExternal = getContentResolver().query(uriExternal, projection, "_data IS NOT NULL", null, null);
Cursor cursorInternal = getContentResolver().query(uriInternal, projection, "_data IS NOT NULL", null, null);
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal, cursorInternal});
while(cursor.moveToNext()){
    path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
    album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
    timestamp = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED));
    countPhoto = Function.getCount(getApplicationContext(), album);
    albumOrPhotoList.add(Function.mappingInbox(album, path, timestamp, Function.converToTime(timestamp), countPhoto));
}
cursor.close();
Collections.sort(albumOrPhotoList,new
// Arranging photo album by timestamp decending
MapComparator(Function.KEY_TIMESTAMP, "dsc")); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM