Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 讀取音樂文件內嵌封面的完整工具類

不少音樂文件自帶專輯封面,Android 可以通過媒體庫提供的 albumart 內容提供者把這張圖取出來。下面封裝成一個 ArtworkUtils 工具類,支持按專輯 ID 取圖、按歌曲 ID 回退取圖,並在失敗時返回自帶的默認佔位圖。

實現思路

先嘗試拼接 content://media/external/audio/albumart 與專輯 ID 得到 Uri,用 ContentResolver 打開輸入流交給 BitmapFactory 解碼;若失敗,再退回使用歌曲 ID 訪問 .../media/{songid}/albumart;最終仍取不到時,根據 allowdefault 參數決定是返回應用內置的 icon 還是 null。

public class ArtworkUtils {

    private static final Uri sArtworkUri =
            Uri.parse("content://media/external/audio/albumart");
    private static final BitmapFactory.Options sBitmapOptions =
            new BitmapFactory.Options();

    public static Bitmap getArtwork(Context context, String title,
                                    long song_id, long album_id,
                                    boolean allowdefault) {
        if (album_id < 0) {
            if (song_id >= 0) {
                Bitmap bm = getArtworkFromFile(context, song_id, -1);
                if (bm != null) {
                    return bm;
                }
            }
            if (allowdefault) {
                return getDefaultArtwork(context);
            }
            return null;
        }
        ContentResolver res = context.getContentResolver();
        Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
        if (uri != null) {
            InputStream in = null;
            try {
                in = res.openInputStream(uri);
                Bitmap bmp = BitmapFactory.decodeStream(in, null, sBitmapOptions);
                if (bmp == null) {
                    bmp = getDefaultArtwork(context);
                }
                return bmp;
            } catch (FileNotFoundException ex) {
                Bitmap bm = getArtworkFromFile(context, song_id, album_id);
                if (bm != null) {
                    if (bm.getConfig() == null) {
                        bm = bm.copy(Bitmap.Config.RGB_565, false);
                        if (bm == null && allowdefault) {
                            return getDefaultArtwork(context);
                        }
                    }
                } else if (allowdefault) {
                    bm = getDefaultArtwork(context);
                }
                return bm;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static Bitmap getArtworkFromFile(Context context,
                                             long songid, long albumid) {
        Bitmap bm = null;
        if (albumid < 0 && songid < 0) {
            throw new IllegalArgumentException(
                    "Must specify an album or a song id");
        }
        try {
            Uri uri;
            if (albumid < 0) {
                uri = Uri.parse("content://media/external/audio/media/"
                        + songid + "/albumart");
            } else {
                uri = ContentUris.withAppendedId(sArtworkUri, albumid);
            }
            ParcelFileDescriptor pfd = context.getContentResolver()
                    .openFileDescriptor(uri, "r");
            if (pfd != null) {
                FileDescriptor fd = pfd.getFileDescriptor();
                bm = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (FileNotFoundException ex) {
            // 忽略,返回 null 由上層決定是否使用默認圖
        }
        return bm;
    }

    private static Bitmap getDefaultArtwork(Context context) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        return BitmapFactory.decodeStream(
                context.getResources().openRawResource(R.drawable.music_icon),
                null, opts);
    }
}

幾點實務提醒

解碼大圖時建議設置 inPreferredConfig 為 RGB_565,能顯著降低內存佔用;另外新版本 Android 對外部存儲與媒體庫的訪問收緊,實際項目中要留意相應的權限與作用域存儲限制。

Copyright © Linux教程網 All Rights Reserved