java 在 Android 中获取专辑封面的最强大方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6591087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most robust way to fetch album art in Android
提问by Ani
Please note that I have already been through similar questions and their answers here and on other websites. I also have a solution that works on some devices (my G2X running CyanogenMod 7.1, my wife's HD2 running a custom ROM and the emulator running Android 2.1). It doesn't, however work on my Nook running CyanogenMod.
请注意,我已经在此处和其他网站上解决了类似的问题及其答案。我还有一个适用于某些设备的解决方案(我的 G2X 运行 CyanogenMod 7.1,我妻子的 HD2 运行自定义 ROM,模拟器运行 Android 2.1)。但是它不适用于运行 CyanogenMod 的 Nook。
My question is: What is the most robust and general way to fetch album art on all android devices? What are the gotchas for specific devices, versions or music applications (I don't mean third-party players, I mean Google Music versus the old Music client)? My current code is:
我的问题是:在所有 android 设备上获取专辑封面的最强大和最通用的方法是什么?特定设备、版本或音乐应用程序的问题是什么(我不是说第三方播放器,我是说谷歌音乐与旧的音乐客户端)?我目前的代码是:
// Is this what's making my code fail on other devices?
public final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
// This works, and well on all devices
private int[] getAlbumIds(ContentResolver contentResolver)
{
List<Integer> result = new ArrayList<Integer>();
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.getContentUri("external"), new String[]{MediaStore.Audio.Media.ALBUM_ID}, null, null, null);
if (cursor.moveToFirst())
{
do{
int albumId = cursor.getInt(0);
if (!result.contains(albumId))
result.add(albumId);
} while (cursor.moveToNext());
}
int[] resultArray = new int[result.size()];
for (int i = 0; i < result.size(); i++)
resultArray[i] = result.get(i);
return resultArray;
}
// This is the bit I want to make more robust, make sure that it works on all devices
private Shader getAlbumArt(ContentResolver contentResolver, int albumId, int width, int height)
{
Uri uri = ContentUris.withAppendedId(sArtworkUri, albumId);
InputStream input = null;
try {
input = contentResolver.openInputStream(uri);
if (input == null)
return null;
Bitmap artwork = BitmapFactory.decodeStream(input);
input.close();
if (artwork == null)
return null;
Bitmap scaled = Bitmap.createScaledBitmap(artwork, width, height, true);
if (scaled == null)
return null;
if (scaled != artwork)
artwork.recycle();
artwork = scaled;
return new BitmapShader(artwork, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Thanks in advance, Ananth
提前致谢,阿南特
回答by Chirag
Here i can attach one function that is return album art from media store . Here in function we just have to pass the album_id which we get from Media store .
在这里我可以附加一个功能,即从媒体商店返回专辑封面。在函数中,我们只需要传递我们从 Media store 获得的专辑 ID。
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
回答by Jawad Zeb
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.javamethod getArtworkQuick.
更完整的示例代码可以在此处的 Android 音乐播放器源中找到https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java方法 getArtworkQuick。
回答by Shubham Rana
I'd like to modify Chirag Raval's answer by using Picasso library. It is simple to use and very powerful.
我想通过使用毕加索库来修改 Chirag Raval 的答案。它使用简单且功能强大。
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, arrayList.get(i).getArt());
Picasso.with(context).load(uri).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)
.into(ivPic);
For full documentation, refer this.
有关完整文档,请参阅此。
回答by 1HaKr
This below code snippet returns the uri for the album art cache present in the MediaStore. may be this will help.
下面的代码片段返回 MediaStore 中存在的专辑封面缓存的 uri。可能这会有所帮助。
Cursor cursorAudio = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, {MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA}, MediaStore.Audio.Media.DATA+ " LIKE \"" + path+ "\"", null, null);if(cursorAudio != null && cursorAudio.moveToFirst()){
Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
cursorAudio.close();
Cursor cursorAlbum = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, MediaStore.Audio.Albums._ID+ "=" + albumId, null, null);
if(cursorAlbum != null && cursorAlbum.moveToFirst()){
String uri = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
cursorAlbum.close();
if(uri != null){
return Uri.parse(uri);
}
}