如何使用MediaStore获取所有音频文件 [英] How to get all the audio file using mediastore

查看:109
本文介绍了如何使用MediaStore获取所有音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从设备(内部和SD卡)中获取所有媒体文件.到目前为止,我已经能够从SD卡的固定文件夹中获取所有媒体文件.mediastore.而且我能够获取媒体信息.但是如何从任何示例中获取设备中的所有音频文件都会有所帮助.我以这种方式尝试了`公共类SongDto {

I am trying to get all the media files from the device (Internal and SDcard).Till now I am able to get all the media files from a fixed folder of SD Card .I have gone through most of the web pages on mediastore .And i am able to get the media info.But how can I get all the audio files from the device any example will be helpful. I tried this way ` 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);
}

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

and getting these value in another class name

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>();

现在我正试图在另一堂课中获得songTitle

and now I am trying to get the the songTitle in another class

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持续时间.以及我在哪里做错了.如何设置getMusicInfos方法`

How I can get only the desired array like array of songId ,songArtist,song duration.And where I am doing wrong .How to set the getMusicInfos method `

推荐答案

您可以尝试

 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来获取内部存储音频文件

and similarly you can also get the internel storage audio files by specifying INTERNEL_CONTENT_URI

这篇关于如何使用MediaStore获取所有音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆