Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android獲取音樂文件的內置專輯圖片實現思路及代碼

android獲取音樂文件的內置專輯圖片實現思路及代碼

編輯:關於Android編程

代碼和注釋如下:
復制代碼 代碼如下:
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);
}
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved