欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android获取音乐文件的内置专辑图片实现思路及代码

程序员文章站 2023-12-03 14:41:28
代码和注释如下: 复制代码 代码如下: private cursor getcursor(string filepath) { string path = null; cu...
代码和注释如下:
复制代码 代码如下:

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