从MediaStore到适配器/列表视图的专辑列表

时间:2013-09-20 22:08:22

标签: android listview adapter mediastore

我可以在mediastore中列出所有歌曲,但我也希望按艺术家/专辑/文件夹/流派/播放列表对其进行排序。

我通常使用游标查询自己的数据库,但这在这种情况下无效,我必须使用适配器。我可以通过更改参数来查询,但我不知道如何在适配器中对它进行分组(我无法找到如何添加/使用GROUPBY),这意味着我需要适配器中的每个艺术家/专辑/文件夹/ ..只有一次,不是每首歌。

我试图通过互联网搜索,但我找不到答案。我在这里也找到了一些主题,但对我来说还不是很清楚。因此,我想请你告诉我example

package com.exercise.AndroidListMedia;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Media;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AndroidListMediaActivity extends ListActivity {

 SimpleCursorAdapter adapter;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  String[] from = {
    MediaStore.MediaColumns.TITLE};
  int[] to = {
    android.R.id.text1};

  Cursor cursor = managedQuery(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    null,
    null,
    null,
    MediaStore.Audio.Media.TITLE);

  adapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_list_item_1, cursor, from, to);
  setListAdapter(adapter);
  }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  Cursor cursor = adapter.getCursor();
  cursor.moveToPosition(position);

  String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
  String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

  String info = "_ID: " + _id + "\n"
 + "TITLE: " + title + "\n"
 + "ARTIST: " + artist + "\n"
 + "ALBUM: " + album + "\n"
 + "DURATION: " + duration/1000 + "s";

  Toast.makeText(this, info, Toast.LENGTH_LONG).show();
 }
}

谢谢你的任何想法。

2 个答案:

答案 0 :(得分:6)

这会让你获得所有专辑:

public Cursor getAlbumAlbumcursor(Context context, Cursor cursor)
{
    String where = null;
    ContentResolver cr = context.getContentResolver();
    final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    final String _id = MediaStore.Audio.Albums._ID; 
    final String album_id = MediaStore.Audio.Albums.ALBUM_ID; 
    final String album_name =MediaStore.Audio.Albums.ALBUM;
    final String artist = MediaStore.Audio.Albums.ARTIST;
    final String[]columns={_id,album_name, artist};
    cursor = cr.query(uri,columns,where,null, null);
    return cursor;
}

这可以为您提供所有曲目:

public Cursor getTrackTrackcursor(Context context, Cursor cursor)
{
    final String track_id = MediaStore.Audio.Media._ID; 
    final String track_no =MediaStore.Audio.Media.TRACK;
    final String track_name =MediaStore.Audio.Media.TITLE;
    final String artist = MediaStore.Audio.Media.ARTIST;
    final String duration = MediaStore.Audio.Media.DURATION;
    final String album = MediaStore.Audio.Media.ALBUM;
    final String composer = MediaStore.Audio.Media.COMPOSER;
    final String year = MediaStore.Audio.Media.YEAR;
    final String path = MediaStore.Audio.Media.DATA;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    ContentResolver cr =  context.getContentResolver();
    final String[]columns={track_id, track_no, artist, track_name,album, duration, path, year, composer};
    cursor = cr.query(uri,columns,null,null,null);
    return cursor;
}

这可以获取所有播放列表:

 public  Cursor getandroidPlaylistcursor(Context context,Cursor cursor)
 {
     ContentResolver resolver = context.getContentResolver();
     final Uri uri=MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
     final String id = MediaStore.Audio.Playlists._ID;
     final String name = MediaStore.Audio.Playlists.NAME;
     final String[]columns = {id,name};
     final String criteria = MediaStore.Audio.Playlists.NAME.length() + " > 0 " ;
     final Cursor crplaylists = resolver.query(uri, columns, criteria, null,name + "    `ASC");
     return crplaylists;
 }

请注意不同的uri用于曲目的MediaStore.Audio.Media,用于专辑详细信息的MediaStore.Audio.Albums,用于播放列表的MediaStore.Audio.Playlists。

cr.query中的最后一个参数是排序顺序,我的播放列表显示了一个示例:name +“`ASC”,按播放列表名称排序。 我希望这有帮助

我开发了一款应用程序,可让您管理/创建播放列表并使用这些技巧。 https://play.google.com/store/apps/details?id=com.flyingdutchman.playlistmanager&hl=en 或者在Google Play上搜索播放列表管理器。

答案 1 :(得分:0)

    public ArrayList<SongBeen> getAlbumList() {

            ArrayList<SongBeen> albumList=new ArrayList<SongBeen>();
            ContentResolver musicResolver = activity.getContentResolver();
            Uri musicUri =    android.provider.MediaStore.Audio.**Albums**.EXTERNAL_CONTENT_URI;
            Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
            if(musicCursor!=null && musicCursor.moveToFirst()){

                int titleColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.TITLE);
                int idColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media._ID);
                int artistColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.ARTIST);
                int albumColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.ALBUM);


                //add songs to list
                do {
                    SongBeen been=new SongBeen();


                    been.setId(musicCursor.getLong(idColumn));
                    been.setAlbum(musicCursor.getString(albumColumn));
                    been.setArtist(musicCursor.getString(artistColumn));


                    albumList.add(been);
            } 
                while (musicCursor.moveToNext());
            }
            return albumList;

        }

注意:

更改相册或我们可以获得的所有其他内容

Uri musicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;