-1

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

請不要downvote它並解決問題呢?更好的方式來使用你的經驗豐富和聰明的大腦是不是? –

回答

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

它的一個片段,我嘗試使用contentResolver的getActivity()方法,但它沒有奏效。 –

+0

請貼上您的Logcat。 –

+0

它找不到符號「contentResolver」。可能是因爲我沒有擴展AppCompatActivity.because因爲它的一個片段。任何其他方式呢?除此之外沒有任何錯誤 –