80,349
社区成员
发帖
与我相关
我的任务
分享
ContentResolver cr = c.getContentResolver();
Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
if (cursor != null && cursor.getCount() > 0) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
long id = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
// 歌曲名
String title = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
// 歌手名
String singer = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
// 歌曲长度
int time = cursor
.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
// 歌曲文件名字
String name = cursor
.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
String suffix = name
.substring(name.length() - 4, name.length());
// 歌曲文件的全路径
String url = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
File file = new File(url);
BufferedReader reader;
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fis);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);
in.reset();
if (first3bytes[0] == (byte) 0xEF
&& first3bytes[1] == (byte) 0xBB
&& first3bytes[2] == (byte) 0xBF) {
charset = "utf-8";
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
charset = "unicode";
Toast.makeText(c, charset, 1).show();
} else if (first3bytes[0] == (byte) 0xFE
&& first3bytes[1] == (byte) 0xFF) {
charset = "utf-16be";
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFF) {
charset = "utf-16le";
} else {
charset = "GBK";
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 专辑名
String album = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
try {
singer = new String(singer.getBytes(), charset);
title = new String(title.getBytes(), charset);
album = new String(album.getBytes(), charset);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long albumid = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
if (url.endsWith(".mp3") || url.endsWith(".MP3")) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID", id);
map.put("TITLE", title);
map.put("SINGER", singer);
map.put("TIME", time);
map.put("NAME", name);
map.put("SUFFIX", suffix);
map.put("URL", url);
map.put("ALBUM", album);
map.put("ALBUMID", albumid);
musicdata.add(map);
}
}