Skip to content

Commit

Permalink
fix: Fixed 'Invalid column COUNT(*) as count' on CameraRoll.getAlbums…
Browse files Browse the repository at this point in the history
…() due to GROUP BY support removed in Android Q. (#176)

Co-authored-by: Antonio Gallo <antgallo@amazon.com>
  • Loading branch information
dokkis and Antonio Gallo committed May 20, 2020
1 parent b0667e2 commit 563ec15
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
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;

Expand Down Expand Up @@ -374,14 +376,12 @@ public void getAlbums(final ReadableMap params, final Promise promise) {
String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_ALL;
StringBuilder selection = new StringBuilder("1");
List<String> 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 + ","
Expand All @@ -394,15 +394,12 @@ public void getAlbums(final ReadableMap params, final Promise promise) {
);
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()]),
Expand All @@ -413,22 +410,34 @@ public void getAlbums(final ReadableMap params, final Promise promise) {
WritableArray response = new WritableNativeArray();
try {
if (media.moveToFirst()) {
Map<String, Integer> albums = new HashMap<>();
do {
String albumName = media.getString(media.getColumnIndex(bucketDisplayName));
int count = media.getInt(media.getColumnIndex("count"));
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<String, Integer> albumEntry : albums.entrySet()) {
WritableMap album = new WritableNativeMap();
album.putString("title", albumName);
album.putInt("count", count);
album.putString("title", albumEntry.getKey());
album.putInt("count", albumEntry.getValue());
response.pushMap(album);
} while (media.moveToNext());
}
}
} 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) {
Expand Down

0 comments on commit 563ec15

Please sign in to comment.