这是抽取专辑的方法,扫描歌曲的时候自己把歌曲的id和专辑的id传进去就能获取
private static final Uri sArtworkUri = Uri
parse("content://media/external/audio/albumart");
/
将MP3里读取出来
@param context
@param songid
@param albumid
@return
/
public static Bitmap getMusicBitemp(Context context, long songid,
long albumid) {
Bitmap bm = null;
// 专辑id和歌曲id小于0说明没有专辑、歌曲,并抛出异常
if (albumid < 0 && songid < 0) {
throw new IllegalArgumentException(
"Must specify an album or a song id");
}
try {
if (albumid < 0) {
Uri uri = Uriparse("content://media/external/audio/media/"
+ songid + "/albumart");
ParcelFileDescriptor pfd = contextgetContentResolver()
openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfdgetFileDescriptor();
bm = BitmapFactorydecodeFileDescriptor(fd);
}
} else {
Uri uri = ContentUriswithAppendedId(sArtworkUri, albumid);
ParcelFileDescriptor pfd = contextgetContentResolver()
openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfdgetFileDescriptor();
bm = BitmapFactorydecodeFileDescriptor(fd);
} else {
return null;
}
}
} catch (FileNotFoundException ex) {
}
return bm;
}
另一种:
// 查找sdcard卡上的所有歌曲信息
public List> getMultiData() {
ArrayList> musicList = new ArrayList>();
// 加入封装音乐信息的代码
// 查询所有歌曲
ContentResolver musicResolver = thisgetContentResolver();
Cursor musicCursor = musicResolverquery(
MediaStoreAudioMediaEXTERNAL_CONTENT_URI, null, null, null,
null);
int musicColumnIndex;
if (null != musicCursor && musicCursorgetCount() > 0) {
for (musicCursormoveToFirst(); !musicCursorisAfterLast(); musicCursor
moveToNext()) {
Map musicDataMap = new HashMap();
Random random = new Random();
int musicRating = Mathabs(randomnextInt()) % 10;
musicDataMapput("musicRating", musicRating);
// 取得音乐播放路径
musicColumnIndex = musicCursor
getColumnIndex(MediaStoreAudioAudioColumnsDATA);
musicPath = musicCursorgetString(musicColumnIndex);
musicDataMapput("musicPath", musicPath);
// 取得音乐的名字
musicColumnIndex = musicCursor
getColumnIndex(MediaStoreAudioAudioColumnsTITLE);
musicName = musicCursorgetString(musicColumnIndex);
musicDataMapput("musicName", musicName);
// 取得音乐的专辑名称
musicColumnIndex = musicCursor
getColumnIndex(MediaStoreAudioAudioColumnsALBUM);
musicAlbum = musicCursorgetString(musicColumnIndex);
musicDataMapput("musicAlbum", musicAlbum);
// 取得音乐的演唱者
musicColumnIndex = musicCursor
getColumnIndex(MediaStoreAudioAudioColumnsARTIST);
musicArtist = musicCursorgetString(musicColumnIndex);
musicDataMapput("musicArtist", musicArtist);
// 取得歌曲对应的专辑对应的Key
musicColumnIndex = musicCursor
getColumnIndex(MediaStoreAudioAudioColumnsALBUM_KEY);
musicAlbumKey = musicCursorgetString(musicColumnIndex);
String[] argArr = { musicAlbumKey };
ContentResolver albumResolver = thisgetContentResolver();
Cursor albumCursor = albumResolverquery(
MediaStoreAudioAlbumsEXTERNAL_CONTENT_URI, null,
MediaStoreAudioAudioColumnsALBUM_KEY + " = ",
argArr, null);
if (null != albumCursor && albumCursorgetCount() > 0) {
albumCursormoveToFirst();
int albumArtIndex = albumCursor
getColumnIndex(MediaStoreAudioAlbumColumnsALBUM_ART);
musicAlbumArtPath = albumCursorgetString(albumArtIndex);
if (null != musicAlbumArtPath
&& !""equals(musicAlbumArtPath)) {
musicDataMapput("musicAlbumImage", musicAlbumArtPath);
} else {
musicDataMapput("musicAlbumImage",
"file:///mnt/sdcard/albjpg");
}
} else {
// 没有专辑定义,给默认
musicDataMapput("musicAlbumImage",
"file:///mnt/sdcard/albjpg");
}
musicListadd(musicDataMap);
}
}
return musicList;
}
用下面这种方式能实现查询实现查询sd卡某一个子目录下的文件详细信息 :
//selection: 指定查询条件
String selection = MediaStoreImagesMediaDATA + " like %";
//设定查询目录
String path="/mnt/sdcard/youpicpath";
//定义selectionArgs:
String[] selectionArgs = {path+"%"};
c = thisgetContentResolver()query(MediaStoreImagesMediaEXTERNAL_CONTENT_URI, null,
selection, selectionArgs, null);
其实原理就是改变了下查询语句,在查询条件中增加了MediaStoreImagesMediaDATA字段的限制条件,必须是和指定目录能匹配的才被查询,注意selection和selectionArgs参数是配合使用的。
Intent intentFromCapture = new Intent( MediaStoreACTION_IMAGE_CAPTURE);
// checkHasSdcard 自定义函数,判断存储卡是否可用
if (checkHasSdcard()) {
File file = new File(“/mnt/SDCard/temp/”);
if (!fileexists())
filemkdirs();
// /mnt/SDCard/temp/tmp_imagejpg 这是拍照后保存的文件,也就是说要自定义拍照后的文件名
intentFromCaptureputExtra(MediaStoreEXTRA_OUTPUT,
UrifromFile(new File(“/mnt/SDCard/temp/”, "tmp_imagejpg")));
}
startActivityForResult(intentFromCapture, 11);
然后在 onActivityResult 里就可以对读取该文件了。
以上就是关于android 怎么获取音乐专辑图像全部的内容,包括:android 怎么获取音乐专辑图像、android如何获取有图片的文件夹列表、android开发 调用系统相机后onActivityResult如何获取刚拍一张图片的名称等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)