From 563ec15239a38e3211450d3af94b5f86debaa1bf Mon Sep 17 00:00:00 2001 From: Antonio Gallo Date: Wed, 20 May 2020 17:59:54 +0200 Subject: [PATCH] fix: Fixed 'Invalid column COUNT(*) as count' on CameraRoll.getAlbums() due to GROUP BY support removed in Android Q. (#176) Co-authored-by: Antonio Gallo --- .../cameraroll/CameraRollModule.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/cameraroll/CameraRollModule.java b/android/src/main/java/com/reactnativecommunity/cameraroll/CameraRollModule.java index 1d2492858..b1aa26930 100644 --- a/android/src/main/java/com/reactnativecommunity/cameraroll/CameraRollModule.java +++ b/android/src/main/java/com/reactnativecommunity/cameraroll/CameraRollModule.java @@ -48,6 +48,8 @@ import java.io.IOException; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.HashMap; import javax.annotation.Nullable; @@ -374,14 +376,12 @@ public class CameraRollModule extends ReactContextBaseJavaModule { String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_ALL; StringBuilder selection = new StringBuilder("1"); List selectionArgs = new ArrayList<>(); - String bucketDisplayName = MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME; if (assetType.equals(ASSET_TYPE_PHOTOS)) { selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = " + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE); } else if (assetType.equals(ASSET_TYPE_VIDEOS)) { selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = " + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO); - bucketDisplayName = MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME; } else if (assetType.equals(ASSET_TYPE_ALL)) { selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " IN (" + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO + "," @@ -394,15 +394,12 @@ public class CameraRollModule extends ReactContextBaseJavaModule { ); return; } - selection.append(") GROUP BY (").append(bucketDisplayName); - String[] projection = new String[]{ - "COUNT(*) as count", - bucketDisplayName, - MediaStore.Images.ImageColumns.DATA - }; + + final String[] projection = {MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME}; + try { Cursor media = getReactApplicationContext().getContentResolver().query( - MediaStore.Files.getContentUri("external").buildUpon().build(), + MediaStore.Files.getContentUri("external"), projection, selection.toString(), selectionArgs.toArray(new String[selectionArgs.size()]), @@ -413,22 +410,34 @@ public class CameraRollModule extends ReactContextBaseJavaModule { WritableArray response = new WritableNativeArray(); try { if (media.moveToFirst()) { + Map albums = new HashMap<>(); do { - String albumName = media.getString(media.getColumnIndex(bucketDisplayName)); - int count = media.getInt(media.getColumnIndex("count")); - WritableMap album = new WritableNativeMap(); - album.putString("title", albumName); - album.putInt("count", count); - response.pushMap(album); + String albumName = media.getString(media.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)); + if (albumName != null) { + Integer albumCount = albums.get(albumName); + if (albumCount == null) { + albums.put(albumName, 1); + } else { + albums.put(albumName, albumCount + 1); + } + } } while (media.moveToNext()); + + for (Map.Entry albumEntry : albums.entrySet()) { + WritableMap album = new WritableNativeMap(); + album.putString("title", albumEntry.getKey()); + album.putInt("count", albumEntry.getValue()); + response.pushMap(album); + } } } finally { media.close(); promise.resolve(response); } } - } catch (Exception e) {} - + } catch (Exception e) { + promise.reject(ERROR_UNABLE_TO_LOAD, "Could not get media", e); + } } private static void putPageInfo(Cursor media, WritableMap response, int limit, int offset) {