feat: get album list (#139)

This commit is contained in:
Tuan Luong
2020-02-08 11:17:56 +08:00
committed by GitHub
parent f3fc48d150
commit fc1855e001
5 changed files with 129 additions and 0 deletions
@@ -370,6 +370,68 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
}
}
@ReactMethod
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 + ","
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + ")");
} else {
promise.reject(
ERROR_UNABLE_TO_FILTER,
"Invalid filter option: '" + assetType + "'. Expected one of '"
+ ASSET_TYPE_PHOTOS + "', '" + ASSET_TYPE_VIDEOS + "' or '" + ASSET_TYPE_ALL + "'."
);
return;
}
selection.append(") GROUP BY (").append(bucketDisplayName);
String[] projection = new String[]{
"COUNT(*) as count",
bucketDisplayName,
MediaStore.Images.ImageColumns.DATA
};
try {
Cursor media = getReactApplicationContext().getContentResolver().query(
MediaStore.Files.getContentUri("external").buildUpon().build(),
projection,
selection.toString(),
selectionArgs.toArray(new String[selectionArgs.size()]),
null);
if (media == null) {
promise.reject(ERROR_UNABLE_TO_LOAD, "Could not get media");
} else {
WritableArray response = new WritableNativeArray();
try {
if (media.moveToFirst()) {
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);
} while (media.moveToNext());
}
} finally {
media.close();
promise.resolve(response);
}
}
} catch (Exception e) {}
}
private static void putPageInfo(Cursor media, WritableMap response, int limit, int offset) {
WritableMap pageInfo = new WritableNativeMap();
pageInfo.putBoolean("has_next_page", limit < media.getCount());