最可靠的方法来获取Android中的专辑封面 [英] Most robust way to fetch album art in Android

查看:207
本文介绍了最可靠的方法来获取Android中的专辑封面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我已经经历过类似的问题和他们的答案在这里和其他网站上。我也有一个解决方案,在某些设备上的工作(我的G2X运行的CyanogenMod 7.1,我妻子的HD2运行自定义ROM和模拟器运行Android 2.1)。这不,但是我的Nook运行CyanogenMod的工作。

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.

我的问题是:什么是最强大和最通用的方式来获取专辑封面上的所有Android设备?什么是陷阱的特定设备,版本或音乐应用程序(我的意思不是第三方的球员,我的意思是谷歌音乐与旧的音乐客户端)?我现在的code是:

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:

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

Thanks in advance, Ananth

推荐答案

在这里我可以附加一个函数,从媒体商店返回专辑封面。在这里,在功能上,我们只需要通过我们从媒体商店获得album_id。

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 .

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

这篇关于最可靠的方法来获取Android中的专辑封面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆