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 <antgallo@amazon.com>
This commit is contained in:
Antonio Gallo 2020-05-20 17:59:54 +02:00 committed by GitHub
parent b0667e2b6b
commit 563ec15239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 17 deletions

View File

@ -48,6 +48,8 @@ import java.io.IOException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -374,14 +376,12 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_ALL; String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_ALL;
StringBuilder selection = new StringBuilder("1"); StringBuilder selection = new StringBuilder("1");
List<String> selectionArgs = new ArrayList<>(); List<String> selectionArgs = new ArrayList<>();
String bucketDisplayName = MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME;
if (assetType.equals(ASSET_TYPE_PHOTOS)) { if (assetType.equals(ASSET_TYPE_PHOTOS)) {
selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = " selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = "
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE); + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE);
} else if (assetType.equals(ASSET_TYPE_VIDEOS)) { } else if (assetType.equals(ASSET_TYPE_VIDEOS)) {
selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = " selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = "
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO); + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO);
bucketDisplayName = MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME;
} else if (assetType.equals(ASSET_TYPE_ALL)) { } else if (assetType.equals(ASSET_TYPE_ALL)) {
selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " IN (" selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " IN ("
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO + "," + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO + ","
@ -394,15 +394,12 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
); );
return; return;
} }
selection.append(") GROUP BY (").append(bucketDisplayName);
String[] projection = new String[]{ final String[] projection = {MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME};
"COUNT(*) as count",
bucketDisplayName,
MediaStore.Images.ImageColumns.DATA
};
try { try {
Cursor media = getReactApplicationContext().getContentResolver().query( Cursor media = getReactApplicationContext().getContentResolver().query(
MediaStore.Files.getContentUri("external").buildUpon().build(), MediaStore.Files.getContentUri("external"),
projection, projection,
selection.toString(), selection.toString(),
selectionArgs.toArray(new String[selectionArgs.size()]), selectionArgs.toArray(new String[selectionArgs.size()]),
@ -413,22 +410,34 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
WritableArray response = new WritableNativeArray(); WritableArray response = new WritableNativeArray();
try { try {
if (media.moveToFirst()) { if (media.moveToFirst()) {
Map<String, Integer> albums = new HashMap<>();
do { do {
String albumName = media.getString(media.getColumnIndex(bucketDisplayName)); String albumName = media.getString(media.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME));
int count = media.getInt(media.getColumnIndex("count")); if (albumName != null) {
WritableMap album = new WritableNativeMap(); Integer albumCount = albums.get(albumName);
album.putString("title", albumName); if (albumCount == null) {
album.putInt("count", count); albums.put(albumName, 1);
response.pushMap(album); } else {
albums.put(albumName, albumCount + 1);
}
}
} while (media.moveToNext()); } while (media.moveToNext());
for (Map.Entry<String, Integer> albumEntry : albums.entrySet()) {
WritableMap album = new WritableNativeMap();
album.putString("title", albumEntry.getKey());
album.putInt("count", albumEntry.getValue());
response.pushMap(album);
}
} }
} finally { } finally {
media.close(); media.close();
promise.resolve(response); 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) { private static void putPageInfo(Cursor media, WritableMap response, int limit, int offset) {