如何使用mediastore获取所有音频文件

时间:2016-05-28 06:08:58

标签: java android file mediastore

我正在尝试从设备中获取所有媒体文件(内部和SD卡)。现在我可以从SD卡的固定文件夹中获取所有媒体文件。我已经浏览了大部分网页mediastore。我可以获取媒体信息。但是如何从设备获取所有音频文件的任何示例都会有所帮助。 我试过这种方式`public class SongDto {

public long songId;
public String songTitle;
public String songArtist;
public String path;
public short genre;
public long duration;
public String album;
public Bitmap albumArt;

public String toString() {
return String.format("songId: %d, Title: %s, Artist: %s, Path: %s, Genere: %d, Duration %s",
        songId, songTitle, songArtist, path, genre, duration);
}

并在另一个类名中获取这些值

public class Utils {
private Context _context;

// constructor
public Utils(Context context) {
    this._context = context;
}
public static ArrayList<SongDto> getMusicInfos(Context context) {

    ArrayList<SongDto> musicInfos = new ArrayList<SongDto>();

现在我想在另一个班级中获得歌曲标题

private ArrayList<SongDto> musicInfos;
private Utils utils;
private ArrayList<String> songPaths = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    utils = new Utils(this);
    songPaths = utils. getMusicInfos(songTitle);
}

private void update(){
    ArrayAdapter<String> list=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,musicInfos.add(songTitle) );
}
}

如何才能获得所需的数组,例如songId,songArtist,song duration.Hnd我在哪里做错了。如何设置getMusicInfos方法 `

2 个答案:

答案 0 :(得分:2)

你可以试试这个

 ArrayList audio=new ArrayList();
 Cursor c=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Audio.Media.DISPLAY_NAME}, null, null, null);


while(c.moveToNext())
 {
String name=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
audio.add(name);

 }

同样您也可以通过指定INTERNEL_CONTENT_URI获取内部存储音频文件

答案 1 :(得分:1)

我使用此代码获取所有歌曲数据

public static ArrayList<SongDto> getMusicInfos(Context context) {

    ArrayList<SongDto> musicInfos = new ArrayList<SongDto>();

    Cursor cursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
            MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    if (cursor == null) {
        return null;
    }


    for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToNext();


        int isMusic = cursor.getInt(cursor
                .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); 

        if (isMusic != 0) {
            SongDto music = new SongDto();


            music.path = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

            if (!new File(music.path).exists()) {
                continue;
            }


            music.songId = cursor.getLong(cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));

            music.songTitle = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));


            music.songTitle = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));


            music.album = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));


            music.songArtist = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));

            music.duration = cursor
                    .getLong(cursor
                            .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));

            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(music.path);
            music.albumArt = getBitmap(mmr.getEmbeddedPicture());
            mmr.release();

            musicInfos.add(music);
        }
    }

    return musicInfos;
}

并将其用作数据对象

public class SongDto {

public long songId;
public String songTitle;
public String songArtist;
public String path;
public short genre;
public long duration;
public String album;
public Bitmap albumArt;

public String toString() {
    return String.format("songId: %d, Title: %s, Artist: %s, Path: %s, Genere: %d, Duration %s",
            songId, songTitle, songArtist, path, genre, duration);
}
}

AbumArt可能为空

将歌曲ID作为列表

获取
 String[] columns = { MediaStore.Audio.Media._ID };
    cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,columns, null, null, null);

从光标中获取数据。要获得内部和外部存储歌曲列表,截至目前我还没有解决方案。