获取sd卡的音乐文件的方法
public class Music { public static ArrayList<MusicMessage> Music(Context context) { ContentResolver resolver = context.getContentResolver(); // 内容提供者访问MediaStore.Audio.Media.EXTERNAL_CONTENT_URI Cursor cursor = resolver.query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); ArrayList<MusicMessage> list = new ArrayList<MusicMessage>(); while (cursor.moveToNext()) { // 名字 String name = cursor.getString(cursor .getColumnIndex(Audio.Media.TITLE)); // 专辑名 String album = cursor.getString(cursor .getColumnIndexOrThrow(Audio.Media.ALBUM)); // 歌手名 String artist = cursor.getString(cursor .getColumnIndexOrThrow(Audio.Media.ARTIST)); // URI歌曲文件存放路径 String path = cursor.getString(cursor .getColumnIndexOrThrow(Audio.Media.DATA)); // 歌曲文件播放时间长度 int duration = cursor.getInt(cursor .getColumnIndexOrThrow(Audio.Media.DURATION)); // 音乐文件大小 int size = cursor.getInt(cursor .getColumnIndexOrThrow(Audio.Media.SIZE)); list.add(new MusicMessage(false, name, album, artist, path, duration, size)); } return list; } }
vo类
public class MusicMessage { public MusicMessage(boolean b, String name, String album, String artist, String path, int duration, int size) { super(); this.b = b; this.name = name; this.album = album; this.artist = artist; this.path = path; this.duration = duration; this.size = size; } @Override public String toString() { return "MusicMessage [b=" + b + ", name=" + name + ", album=" + album + ", artist=" + artist + ", path=" + path + ", duration=" + duration + ", size=" + size + "]"; } public boolean isB() { return b; } public void setB(boolean b) { this.b = b; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public boolean b; public String name; public String album; public String artist; public String path; public int duration; public int size; }
调用方法
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<MusicMessage> list = Music.Music(MainActivity.this); System.out.println(list.size()); System.out.println(list.toString()); ListView listview = (ListView) findViewById(R.id.listview); listview.setAdapter(new Myadapter(list, MainActivity.this)); } }