-3

私はアンドロイドが初めてです。私はサービスを使って音楽プレーヤーを実装しています。ここでは、エラーが集中しているコードを示します。誰もこのエラーを解決するために私を助けることができますか?IllegalStateException:行0、列-1をCursorWindowから読み取ることができませんでした。カーソルがそこからデータにアクセスする前に正しく初期化されていることを確認してください

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

あなたのcusrorはnullです。カーソルがいっぱいになるように十分なメディアがあることを確認してください。 –

+0

ええ、私はそれが十分なメディアを持っているので、カーソルがnullにならないようにします – 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()); 
} 

をそして、私は以下のようにアルバムを取得するためのサービスクラスにplaySong()にgetsongImage(URI)メソッドを呼び出すことで試してみました:

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

関連する問題

 関連する問題