簡體   English   中英

按字母順序獲取所有歌曲的專輯列表

[英]Get the album list of all the songs in an alphabetical order

這是我寫的代碼,錯誤是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;
            }

}

代碼將是

你應該像這樣查詢專輯

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM