1

Mediastoreを使用しているデバイスですべての音楽ファイルを取得しようとしています。 すべてのファイルを非同期で取得していますが、すべての曲を取得するのに6〜7秒かかるので、その間にユーザーはイン・デタント・プログレスバーを見なければなりません。私はそれぞれの曲Mediastoreクエリの簡略化

のAlbumArtを取得するために、クエリ内のクエリを使用していた

は、高度な

答えて

0
でそれに対処する方法やより良いアプローチ

public ArrayList<SongList> getSongs() { 
     ContentResolver musicResolver = context.getContentResolver(); 
     Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 

     Cursor musicCursor = musicResolver.query(musicUri, new String[] {MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media._ID 
     ,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.ALBUM,MediaStore.Audio.Media.ALBUM_ID, 
       MediaStore.Audio.Media.COMPOSER,MediaStore.Audio.Media.YEAR,MediaStore.Audio.Media.TRACK 
     }, null, null, MediaStore.Audio.Media.TITLE); 

     if (musicCursor != null && musicCursor.moveToFirst()) { 
      //int value=0; 
      //get columns 
      int titleColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.TITLE); 
      int idColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media._ID); 
      int artistColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.ARTIST); 
      int pathColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.DATA); 
      int albumColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.ALBUM); 
      int albumIDColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.ALBUM_ID); 
      int composerColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.COMPOSER); 
      int yearColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.YEAR); 
      int trackColumn = musicCursor.getColumnIndex 
        (MediaStore.Audio.Media.TRACK); 
      String thisAlbumArt = ""; 

      //add songs to list 
      do { 
       long thisId = musicCursor.getLong(idColumn); 
       String thisTitle = musicCursor.getString(titleColumn); 
       String thisArtist = musicCursor.getString(artistColumn); 
       String thisPath = musicCursor.getString(pathColumn); 
       String thisAlbum = musicCursor.getString(albumColumn); 
       long thisalbumID = musicCursor.getLong(albumIDColumn); 
       String thisComposer = musicCursor.getString(composerColumn); 
       int thisyear = musicCursor.getInt(yearColumn); 
       int track = musicCursor.getInt(trackColumn); 

       songQueryList.add(thisTitle); 
       Cursor cursor = musicResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
         new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
         MediaStore.Audio.Albums._ID + "=?", 
         new String[]{musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))}, 
         null); 
       if (cursor!=null && cursor.moveToFirst()) { 

        thisAlbumArt = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
        cursor.close(); 
       } 
       if(thisPath.endsWith(".mp3")|| thisPath.endsWith(".m4p") || thisPath.endsWith(".wav")) { 

        songs.add(new SongList(thisId, thisTitle, thisAlbum, thisArtist, thisPath, thisAlbumArt ,thisyear,track ,thisComposer ,thisalbumID)); 
       } 
       else{ 

       } 
       //cursor.moveToNext(); 
      } 
      while (musicCursor.moveToNext()); 
     } 
     if(musicCursor!=null) { 
      musicCursor.close(); 
     } 
     //Log.e("HI"," "+musicCursor.isClosed()); 

     /*Collections.sort(songs, new Comparator<SongList>(){ 
      public int compare(SongList a, SongList b){ 
       return a.getTitle().compareToIgnoreCase(b.getTitle()); 
      } 
     });*/ 

     return songs; 

    } 

ありがとうはあります

私は解決策を見つけました。実際にこの数行のコードは、私が内部で呼んでいた遅延を引き起こします。do-whileループ

Cursor cursor = musicResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
        new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
        MediaStore.Audio.Albums._ID + "=?", 
        new String[]{musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))}, 
        null); 

私は、カーソルを照会することは非常に高価で、時間がかかるので、複数回使用すると遅延が生じることを追加したいと思います。

むしろ、SONG、ALBUM、ARTISTの3つの異なるクエリを使用して、それらを相互に関連付けるロジックを使用しました。

これが役に立ちます。 ハッピーコーディング。

0

次の2つの検索方法があります。最初はアルバムのIDを使用し、2番目はアルバムのIDを使用します。

// requires WRITE_EXTERNAL_STORAGE on N+ 
public void loadAlbumArt(int albumId, ImageView view){ 
    Uri artworkUri = Uri.parse("content://media/external/audio/albumart"); 
    Uri path = ContentUris.withAppendedId(artworkUri, albumId); 
    Glide.with(view.getContext()).load(path).into(view); 
} 

public static void loadAlbumArt(int songId, ImageView view){ 
    Uri artworkUri = Uri.parse("content://media/external/audio/media/" + songId + "/albumart"); 
    Glide.with(view.getContext()).loadFromMediaStore(artworkUri).into(view); 
}