这是我写的代码,错误是getContentResolver().query(MediaStore.Audio.Media.Album.EXTERNAL_CONTENT_URI, proj, null, null, null)
有错误,无法解析相册。这是获取专辑列表的正确方法还是有其他更好的方法?我想为流派,艺术家做同样的事情,所以如果他们有不同的方式,请告诉我们:
public class AlbumsFragment extends Fragment{
public AlbumsFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_albums, container, false);
ListView albumView=(ListView)view.findViewById(R.id.albumsView);
ArrayList<String> albumList = new ArrayList<>();
String[] proj = {MediaStore.Audio.Media.ALBUM};
final Cursor albumCursor = getActivity().getContentResolver().query(MediaStore.Audio.Media.Album.EXTERNAL_CONTENT_URI, proj, null, null, null);
if (albumCursor != null) {
if (albumCursor.moveToFirst()) {
do {
int albumIndex = albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
albumList.add(albumCursor.getString(albumIndex));
} while (albumCursor.moveToNext());
}
}
albumCursor.close();
return view;
}
}
答案 0 :(得分:1)
代码将是
您应该像这样查询相册
String[] projection = new String[] { Albums._ID, Albums.ALBUM, Albums.ARTIST, Albums.ALBUM_ART, Albums.NUMBER_OF_SONGS };
String selection = null;
String[] selectionArgs = null;
String sortOrder = Media.ALBUM + " COLLATE NOCASE ASC";
Cursor cursor = contentResolver.query(Albums.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, sortOrder);