android获取音乐文件的内置专辑图片实现思路及代码
更新时间:2013年06月03日 16:31:27 作者:
获取音乐文件的内置专辑图片这是在播放音乐时的一个很不错的功能,下面与大家分享下具体的实现思路,有类似需求的朋友可以参考下哈
代码和注释如下:
private Cursor getCursor(String filePath) {
String path = null;
Cursor c = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
// System.out.println(c.getString(c.getColumnIndex("_data")));
if (c.moveToFirst()) {
do {
// 通过Cursor 获取路径,如果路径相同则break;
System.out.println("////////"+filePath);
path = c.getString(c
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
System.out.println("?????????"+path);
// 查找到相同的路径则返回,此时cursorPosition 便是指向路径所指向的Cursor 便可以返回了
if (path.equals(filePath)) {
// System.out.println("audioPath = " + path);
// System.out.println("filePath = " + filePath);
// cursorPosition = c.getPosition();
break;
}
} while (c.moveToNext());
}
// 这两个没有什么作用,调试的时候用
// String audioPath = c.getString(c
// .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//
// System.out.println("audioPath = " + audioPath);
return c;
}
private String getAlbumArt(int album_id) {
String mUriAlbums = "content://media/external/audio/albums";
String[] projection = new String[] { "album_art" };
Cursor cur = this.getContentResolver().query(
Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
projection, null, null, null);
String album_art = null;
if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
cur.moveToNext();
album_art = cur.getString(0);
}
cur.close();
cur = null;
return album_art;
}
private void getImage(){
Cursor currentCursor = getCursor("/mnt/sdcard/"+mp3Info);
int album_id = currentCursor.getInt(currentCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
String albumArt = getAlbumArt(album_id);
Bitmap bm = null;
if (albumArt == null) {
mImageView.setBackgroundResource(R.drawable.staring);
} else {
bm = BitmapFactory.decodeFile(albumArt);
BitmapDrawable bmpDraw = new BitmapDrawable(bm);
mImageView.setImageDrawable(bmpDraw);
}
}
复制代码 代码如下:
private Cursor getCursor(String filePath) {
String path = null;
Cursor c = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
// System.out.println(c.getString(c.getColumnIndex("_data")));
if (c.moveToFirst()) {
do {
// 通过Cursor 获取路径,如果路径相同则break;
System.out.println("////////"+filePath);
path = c.getString(c
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
System.out.println("?????????"+path);
// 查找到相同的路径则返回,此时cursorPosition 便是指向路径所指向的Cursor 便可以返回了
if (path.equals(filePath)) {
// System.out.println("audioPath = " + path);
// System.out.println("filePath = " + filePath);
// cursorPosition = c.getPosition();
break;
}
} while (c.moveToNext());
}
// 这两个没有什么作用,调试的时候用
// String audioPath = c.getString(c
// .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//
// System.out.println("audioPath = " + audioPath);
return c;
}
private String getAlbumArt(int album_id) {
String mUriAlbums = "content://media/external/audio/albums";
String[] projection = new String[] { "album_art" };
Cursor cur = this.getContentResolver().query(
Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
projection, null, null, null);
String album_art = null;
if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
cur.moveToNext();
album_art = cur.getString(0);
}
cur.close();
cur = null;
return album_art;
}
private void getImage(){
Cursor currentCursor = getCursor("/mnt/sdcard/"+mp3Info);
int album_id = currentCursor.getInt(currentCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
String albumArt = getAlbumArt(album_id);
Bitmap bm = null;
if (albumArt == null) {
mImageView.setBackgroundResource(R.drawable.staring);
} else {
bm = BitmapFactory.decodeFile(albumArt);
BitmapDrawable bmpDraw = new BitmapDrawable(bm);
mImageView.setImageDrawable(bmpDraw);
}
}
相关文章
Android编程滑动效果之Gallery仿图像集浏览实现方法
这篇文章主要介绍了Android编程滑动效果之Gallery仿图像集浏览实现方法,结合实例形式详细分析了Gallery浏览图片的原理、步骤与相关实现技巧,需要的朋友可以参考下2016-02-02
最新评论