在Android中获取相册艺术的最强大方式



请注意,我已经在这里和其他网站上遇到过类似的问题及其答案。我也有一个解决方案,可以在一些设备上工作(我的G2X运行CyanogenMod 7.1,我妻子的HD2运行自定义ROM和模拟器运行Android 2.1(。然而,它不适用于我的Nook运行CyanogeneMod。

我的问题是:在所有安卓设备上获取相册艺术的最强大和最通用的方法是什么?对于特定的设备、版本或音乐应用程序(我不是指第三方播放器,我是指谷歌音乐与旧的音乐客户端(,有什么问题?我当前的代码是:

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

提前感谢,Ananth

这里我可以附加一个功能,即从媒体商店返回相册艺术。在函数中,我们只需要传递从媒体商店获得的album_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;
}
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);

更多完整的示例代码可以在这里的Android音乐播放器源代码中找到https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java方法getArtworkQuick。

下面的代码片段返回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);
            }
        }

我想使用Picasso库修改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);

有关完整文档,请参阅。

相关内容

  • 没有找到相关文章

最新更新