获取本地MP3的封面

查询手机中的MP3文件,其数据返回到Cursor中,获取MediaStore.Audio.Media.ALBUM_ID这列的数据,通过getAlbumArt方法得到封面图片

Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,

MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
while (cursor.moveToNext()) {
if (cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_MUSIC)).equals(""))
continue;
int album_id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Drawable album_img = getAlbumArt(album_id);

}

/**

* 功能 通过album_id查找 album_art 如果找不到返回null

* @param album_id
* @return album_art
*/
private Drawable 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;
if (album_art == null || album_art.equals("")) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ablum_deflaut);
BitmapDrawable bmpDraw = new BitmapDrawable(bm);
return bmpDraw;
}
Bitmap bm = BitmapFactory.decodeFile(album_art);
BitmapDrawable bmpDraw = new BitmapDrawable(bm);
return bmpDraw;
}

你可能感兴趣的:(Android,null,string,手机)