不负相思意
2018-11-02 21:03:19浏览 3056
这是抽取专辑图片的方法,扫描歌曲的时候自己把歌曲的id和专辑的id传进去就能获取
private static final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
/**
* 将MP3里图片读取出来
*
* @param context
* @param songid
* @param albumid
* @return
*/
public static Bitmap getMusicBitemp(Context context, long songid,
long albumid) {
Bitmap bm = null;
// 专辑id和歌曲id小于0说明没有专辑、歌曲,并抛出异常
if (albumid < 0 && songid < 0) {
throw new IllegalArgumentException(
"Must specify an album or a song id");
}
try {
if (albumid < 0) {
Uri uri = Uri.parse("content://media/external/audio/media/"
+ songid + "/albumart");
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} else {
Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
} else {
return null;
}
}
} catch (FileNotFoundException ex) {
}
return bm;
}
另一种:
// 查找sdcard卡上的所有歌曲信息
public List> getMultiData() {
ArrayList> musicList = new ArrayList>();
// 加入封装音乐信息的代码
// 查询所有歌曲
ContentResolver musicResolver = this.getContentResolver();
Cursor musicCursor = musicResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
int musicColumnIndex;
if (null != musicCursor && musicCursor.getCount() > 0) {
for (musicCursor.moveToFirst(); !musicCursor.isAfterLast(); musicCursor
.moveToNext()) {
Map musicDataMap = new HashMap();
Random random = new Random();
int musicRating = Math.abs(random.nextInt()) % 10;
musicDataMap.put("musicRating", musicRating);
// 取得音乐播放路径
musicColumnIndex = musicCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
musicPath = musicCursor.getString(musicColumnIndex);
musicDataMap.put("musicPath", musicPath);
// 取得音乐的名字
musicColumnIndex = musicCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
musicName = musicCursor.getString(musicColumnIndex);
musicDataMap.put("musicName", musicName);
// 取得音乐的专辑名称
musicColumnIndex = musicCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);
musicAlbum = musicCursor.getString(musicColumnIndex);
musicDataMap.put("musicAlbum", musicAlbum);
// 取得音乐的演唱者
musicColumnIndex = musicCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
musicArtist = musicCursor.getString(musicColumnIndex);
musicDataMap.put("musicArtist", musicArtist);
// 取得歌曲对应的专辑对应的Key
musicColumnIndex = musicCursor
.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_KEY);
musicAlbumKey = musicCursor.getString(musicColumnIndex);
String[] argArr = { musicAlbumKey };
ContentResolver albumResolver = this.getContentResolver();
Cursor albumCursor = albumResolver.query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,
MediaStore.Audio.AudioColumns.ALBUM_KEY + " = ?",
argArr, null);
if (null != albumCursor && albumCursor.getCount() > 0) {
albumCursor.moveToFirst();
int albumArtIndex = albumCursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);
musicAlbumArtPath = albumCursor.getString(albumArtIndex);
if (null != musicAlbumArtPath
&& !"".equals(musicAlbumArtPath)) {
musicDataMap.put("musicAlbumImage", musicAlbumArtPath);
} else {
musicDataMap.put("musicAlbumImage",
"file:///mnt/sdcard/alb.jpg");
}
} else {
// 没有专辑定义,给默认图片
musicDataMap.put("musicAlbumImage",
"file:///mnt/sdcard/alb.jpg");
}
musicList.add(musicDataMap);
}
}
return musicList;
}
long id = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media._ID)); //音乐id
String artist = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家
String album = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM)); //专辑
long albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
long duration = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长
long size = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
String url = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
int isMusic = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐
if (isMusic != 0) { // 只把音乐添加到集合当中
}
原文链接:http://www.apkbus.com/blog-85568-59736.html