如何从 Android Q 中的 mediastore 获取相册?
How to get albums from mediastore in Android Q?
我正在尝试从媒体商店(相册方面)获取图像和视频。它在 OS 版本 < 10 的设备中正常工作。在 Android 问我遇到语法错误.
System.err: android.database.sqlite.SQLiteException: near "GROUP": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT _id, _data, date_modified, bucket_display_name, media_type FROM files WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND ((media_type=1 OR media_type=3) GROUP BY (bucket_display_name)) ORDER BY bucket_display_name ASC
我正在使用以下代码获取数据。
Uri queryUri = MediaStore.Files.getContentUri("external");
Uri intqueryUri = MediaStore.Files.getContentUri("internal");
String[] projection = {MediaStore.Images.Media._ID,
MediaStore.MediaColumns.DATA,
MediaStore.MediaColumns.DATE_MODIFIED,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Files.FileColumns.MEDIA_TYPE};
// Return only video and image metadata.
String selection;
selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
Cursor cursorExternal,cursorInternal;
cursorExternal = getContentResolver().query(queryUri, projection, selection+") GROUP BY (bucket_display_name",
null, MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" ASC");
cursorInternal = getContentResolver().query(intqueryUri, projection, selection+") GROUP BY (bucket_display_name",
null, MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" ASC");
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal, cursorInternal});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
Log.w( "album:** ",album+"" );
HashMap<String, String> listData = Function.mappingInboxAlbum(album, path, countPhoto);
if (!albumList.contains(listData)) {
albumList.add(listData);
}
}
cursor.close();
我用谷歌搜索但没有得到任何解决方案。
请帮忙...
这是我的方法:
首先你需要 album_id
Uri path = Uri
.parse("content://media/external/audio/albumart");
Bitmap bmp = Album.getAlbumart(getActivity(), Long.parseLong(album_id), path);
然后
public Bitmap getAlbumart(Context context, long album_id, Uri sArtworkUri) {
Bitmap bm = null;
try {
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
终于要显示使用Glide库了
try {
Glide.with(mContext)
.load(uri)
.asbitmap
.error(Glide.with(ivcircle).load(R.drawable.playlist))
.into(ivcircle);
} catch (Exception e) {
e.printStackTrace();
}
在 AndroidManifest 中添加这一行:
android:requestLegacyExternalStorage="true"
you can use this method
public static ArrayList<Album> loadAlbum(Context context) {
ContentResolver contentResolver = context.getContentResolver();
ArrayList<Album> albumList = new ArrayList<>();
Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
String[] projection = new String[]
{
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.NUMBER_OF_SONGS
};
//String sortOrder = MediaStore.Audio.Albums.ALBUM + " ASC";
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST));
int numberOfSongs = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS));
// Save to albumList
albumList.add(new Album(title, artist, numberOfSongs));
}
}
assert cursor != null;
cursor.close();
return albumList;
}
我正在尝试从媒体商店(相册方面)获取图像和视频。它在 OS 版本 < 10 的设备中正常工作。在 Android 问我遇到语法错误.
System.err: android.database.sqlite.SQLiteException: near "GROUP": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT _id, _data, date_modified, bucket_display_name, media_type FROM files WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND ((media_type=1 OR media_type=3) GROUP BY (bucket_display_name)) ORDER BY bucket_display_name ASC
我正在使用以下代码获取数据。
Uri queryUri = MediaStore.Files.getContentUri("external");
Uri intqueryUri = MediaStore.Files.getContentUri("internal");
String[] projection = {MediaStore.Images.Media._ID,
MediaStore.MediaColumns.DATA,
MediaStore.MediaColumns.DATE_MODIFIED,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Files.FileColumns.MEDIA_TYPE};
// Return only video and image metadata.
String selection;
selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
Cursor cursorExternal,cursorInternal;
cursorExternal = getContentResolver().query(queryUri, projection, selection+") GROUP BY (bucket_display_name",
null, MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" ASC");
cursorInternal = getContentResolver().query(intqueryUri, projection, selection+") GROUP BY (bucket_display_name",
null, MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" ASC");
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal, cursorInternal});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
Log.w( "album:** ",album+"" );
HashMap<String, String> listData = Function.mappingInboxAlbum(album, path, countPhoto);
if (!albumList.contains(listData)) {
albumList.add(listData);
}
}
cursor.close();
我用谷歌搜索但没有得到任何解决方案。 请帮忙...
这是我的方法:
首先你需要 album_id
Uri path = Uri
.parse("content://media/external/audio/albumart");
Bitmap bmp = Album.getAlbumart(getActivity(), Long.parseLong(album_id), path);
然后
public Bitmap getAlbumart(Context context, long album_id, Uri sArtworkUri) {
Bitmap bm = null;
try {
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
终于要显示使用Glide库了
try {
Glide.with(mContext)
.load(uri)
.asbitmap
.error(Glide.with(ivcircle).load(R.drawable.playlist))
.into(ivcircle);
} catch (Exception e) {
e.printStackTrace();
}
在 AndroidManifest 中添加这一行:
android:requestLegacyExternalStorage="true"
you can use this method
public static ArrayList<Album> loadAlbum(Context context) {
ContentResolver contentResolver = context.getContentResolver();
ArrayList<Album> albumList = new ArrayList<>();
Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
String[] projection = new String[]
{
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.NUMBER_OF_SONGS
};
//String sortOrder = MediaStore.Audio.Albums.ALBUM + " ASC";
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST));
int numberOfSongs = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS));
// Save to albumList
albumList.add(new Album(title, artist, numberOfSongs));
}
}
assert cursor != null;
cursor.close();
return albumList;
}