android获取本地音乐的专辑图片

获取本地的音文件请查看 http://blog.csdn.net/zdnuist/article/details/42393947

获取专辑图片主要是通过album_id进行查询,因此首先获取album_id

String album_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

Sdcard上的专辑图片存储位置为 /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/

接下来获取album_art

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

获取出来的值就是图片的存储地址,最后获取图片

Bitmap bm = null; 
					bm = BitmapFactory.decodeFile(
album_art
); iv1.setImageBitmap(bm);
 
  

最后补充下扫描sdcard音乐文件的方法:

//4.4以前可以用MEDIA_MOUNTED  4.4以后把它变为系统权限了  不能用
        if(android.os.Build.VERSION.SDK_INT < 19){
        	mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()))); 
		} else {
			MediaScannerConnection.scanFile(mContext,  
	                  new String[] { Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator }, null,  
	                  new MediaScannerConnection.OnScanCompletedListener() {  
	              public void onScanCompleted(String path, Uri uri) {  
	              }  
	         });
		}




你可能感兴趣的:(ANDROID杂记)