-3

我是新來的android。我正在使用服務實現音樂播放器。這裏是錯誤集中的代碼。任何人都可以幫我解決這個錯誤嗎?IllegalStateException:無法從CursorWindow讀取行0,列-1。確保光標在從其訪問數據前正確初始化

public void getSong() { 
    ContentResolver contentResolver = getContentResolver(); 
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Cursor cursor = contentResolver.query(musicUri, null, null, null, null); 

    if (cursor != null && cursor.moveToFirst()) { 
     do { 
      long thisId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); 
      String thisTitle = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); 
      String thisArtist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); 

      int albumId = cursor.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART); 
      String thisArt = cursor.getString(albumId); 

      musicArrayList.add(new Music(thisId, thisTitle, thisArtist, thisArt)); 
     } while (cursor.moveToNext()); 
    } 
} 
+0

您的錯誤爲空。確保你有足夠的媒體,以便遊標填滿。 –

+0

是的,我檢查,它有足夠的媒體,所以光標不能爲空 – Janki

+0

在哪一行是這個錯誤? –

回答

0

檢查空數據。

ListView audioListView = (ListView) findViewById(R.id.songView); 

    ArrayList<string> audioList = new ArrayList<>(); 

    String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME };// Can include more data for more details and check it. 

    Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null); 

    if(audioCursor != null){ 
     if(audioCursor.moveToFirst()){ 
      do{ 
       int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); 

       audioList.add(audioCursor.getString(audioIndex)); 
      }while(audioCursor.moveToNext()); 
     } 
    } 
    audioCursor.close(); 

    ArrayAdapter<string> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, audioList); 
    audioListView.setAdapter(adapter); 
0

錯誤已解決,因爲我在尋找特定歌曲的專輯,所以在遊標中傳遞null值。所以我改變了我的如下光標代碼:

public void getSong() { 

     ContentResolver contentResolver = getContentResolver(); 
     Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
       new String[]{MediaStore.Audio.Media._ID, 
         MediaStore.Audio.Media.TITLE, 
         MediaStore.Audio.Media.ARTIST, 
         MediaStore.Audio.Media.ALBUM}, 
       MediaStore.Audio.Media.IS_MUSIC + "=1", 
       null, 
       null); 


     if (cursor != null && cursor.moveToFirst()) { 
      do { 
       long thisId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)); 
       String thisTitle = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); 
       String thisArtist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); 
       String thisImage = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); 

       songList.add(new Music(thisId, thisTitle, thisArtist, thisImage)); 
}while (cursor.moveToNext()); 
} 

而且我在服務類調用getsongImage(URI)方法playSong()嘗試了獲取相冊如下:

public void getSongImage(Uri uri) { 
     MediaMetadataRetriever mmr = new MediaMetadataRetriever(); 
     mmr.setDataSource(this,uri); 
     byte [] data = mmr.getEmbeddedPicture(); 
     if(data==null){ 
      activitySongDetailBinding.imgMusic.setImageResource(R.drawable.img); 
     }else { 
      Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
      activitySongDetailBinding.imgMusic.setImageBitmap(bitmap); 
     } 
    } 

其中URI = ContentUris.withAppendedId( android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songsArrayList.get(songPosn).getId())

相關問題