Instagram应用使用MediaStore.Images.Media.EXTERNAL_CONTENT_URI查询设备上存储的图像。以下是获取相册列表的代码示例:
public List getAllAlbums(Context context) {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " <> ?";
String[] selectionArgs = new String[] {"Screenshots"};
String orderBy = MediaStore.Images.Media.DATE_MODIFIED;
List albumList = new ArrayList<>();
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, orderBy);
if (cursor != null) {
while (cursor.moveToNext()) {
String bucketId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID));
String bucketName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
// add album to list
albumList.add(new Album(bucketId, bucketName));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return albumList;
}
该代码在查询时,使用了“SCREENSHOTS”来过滤相册列表,因为Instagram不想让用户在相册列表中看到截屏。通过遍历cursor,将每个相册的bucketId和bucketName存储到一个Album对象列表中,并将其返回给调用方。