1

Android DBへのクエリを作成するのを手伝ってくれる人はいますか? 選択したフォルダ内のすべてのアルバムを選択する必要があります。すべての曲について、私はこれを作ります:

  private Cursor allSongs() {
    String[] projection = {MediaStore.Audio.Media._ID,             // 0
            MediaStore.Audio.Media.ARTIST,          // 1
            MediaStore.Audio.Media.TITLE,           // 2
            MediaStore.Audio.Media.ALBUM_ID,  // 3
            MediaStore.Audio.Media.ALBUM,           // 4
            MediaStore.Audio.Media.DATA,            // 5
            MediaStore.Audio.Media.DISPLAY_NAME,    // 6
            MediaStore.Audio.Media.DURATION,     //7
            MediaStore.Audio.Media.TITLE_KEY        //8
    };

    String selection = android.provider.MediaStore.Audio.Media.DATA + " like ? ";
    String MEDIA_PATH = Environment.getExternalStorageDirectory() + "/Music/";

    Cursor musicListSDCardCursor = mQuery(
            getActivity(),
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection, selection, new String[]{"%" + MEDIA_PATH + "%"}, null, 0);
    return musicListSDCardCursor;
}

および mQuery コード:

 public static Cursor mQuery(Context context, Uri uri, String[] projection,
                            String selection, String[] selectionArgs, String sortOrder, int limit) {
    try {
        ContentResolver resolver = context.getContentResolver();
        if (resolver == null) {
            return null;
        }
        if (limit > 0) {
            uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
        }
        return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
    } catch (UnsupportedOperationException ex) {
        return null;
    }

}

しかし、このフォルダ /sdcard/Music/ からすべてのアルバムを取得するために MediaStore にクエリを実行するにはどうすればよいでしょうか?


私はそれを次のようにします:

 private Cursor allAlbums() {
    Cursor cursor_albums_from_songs = allSongs();

    HashSet<String> albumsList = new HashSet<String>();
    int count = cursor_albums_from_songs.getCount();
    for (int i = 0; i < count; i++) {
        cursor_albums_from_songs.moveToPosition(i);
        albumsList.add(cursor_albums_from_songs.getString(3));
        Log.i("cursor", "" + cursor_albums_from_songs.getString(3));
    }
    Log.i("allAlbums", "" + albumsList.size());
    ArrayList<String> list = new ArrayList<String>(albumsList.size());
    list.addAll(albumsList);
    int count_ids = list.size();
    Cursor[] cursors = new Cursor[count_ids];
    for (int i = 0; i < count_ids; i++) {
        String selection = MediaStore.Audio.Albums._ID + " = ? ";
        String[] selectionArgs = new String[]{list.get(i)};

        String[] projection = {MediaStore.Audio.Albums._ID,  //0
                MediaStore.Audio.Albums.ALBUM,               // 1
                MediaStore.Audio.Albums.ALBUM_ART,           // 2
                MediaStore.Audio.Albums.ARTIST,              //3
                MediaStore.Audio.Albums.FIRST_YEAR,           //4
                MediaStore.Audio.Albums.NUMBER_OF_SONGS      //5
        };

        Cursor artistListSDCardCursor = mQuery(
                mContext,
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                projection, selection, selectionArgs, null, 0);
        cursors[i] = artistListSDCardCursor;
    }
    MergeCursor mMergeCursor = new MergeCursor(cursors);
    return mMergeCursor;
}
4

0 に答える 0