使用CursorLoader查询专辑的歌曲

时间:2012-06-16 16:58:12

标签: android android-cursor android-cursorloader

我希望通过MediaStore

查询CursorLoader来获取相册的歌曲列表

我该怎么做?我可以使用以下代码获取设备的所有歌曲:

static final String[] TRACK_SUMMARY_PROJECTION = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE}; 

public Loader<Cursor> onCreateLoader(int id, Bundle args) {  
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
    String select = null;  
    return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  
                    TRACK_SUMMARY_PROJECTION, select, null,  
                    sortOrder);  
}  

我应该在代码或修改中添加什么来过滤特定相册的歌曲?

1 个答案:

答案 0 :(得分:20)

一步一步走

步骤1查看手机上加载的相册名称

为相册信息请求光标

String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
        android.provider.MediaStore.Audio.Albums.ALBUM };

cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
        columns, null, null, null);

步骤2找到所有相册名称后。您可以记下所需的相册名称并查询其中的歌曲

请求包含特定专辑的歌曲信息的光标

String[] columns = { MediaStore.Audio.Media.DATA,
          MediaStore.Audio.Media._ID,
          MediaStore.Audio.Media.TITLE,
          MediaStore.Audio.Media.DISPLAY_NAME,
          MediaStore.Audio.Media.MIME_TYPE, };

      String where = android.provider.MediaStore.Audio.Media.ALBUM
          + "=?";

      String whereVal[] = { Album name from which you want songs };

      String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

      cursor = managedQuery(
          MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
          where, whereVal, orderBy);

现在返回此光标。

以下是您的参考资料,用于检索相册名称及其中的所有歌曲。

package org.vipul;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
                android.provider.MediaStore.Audio.Albums.ALBUM };

        Cursor cursor = managedQuery(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, null,
                null, null);

        if (cursor.moveToFirst()) {
            do {
                Log.v("Vipul",
                        cursor.getString(cursor
                                .getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM)));
            } while (cursor.moveToNext());
        }

        // I want to list down song in album Rolling Papers (Deluxe Version)

        String[] column = { MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE, };

        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";

        String whereVal[] = { "Rolling Papers (Deluxe Version)" };

        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

        cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                column, where, whereVal, orderBy);

        if (cursor.moveToFirst()) {
            do {
                Log.v("Vipul",
                        cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
            } while (cursor.moveToNext());
        }

    }
}