Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - How to get all the audio file using mediastore

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

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

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 `

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this

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

 }

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...