从MP3文件或文件输入流中获取位图(专辑艺术)是否可能?

3

是否可以从文件路径或文件输入流中检索mp3文件的位图(专辑封面),例如使用BitmapFactory API或其他API。


你有检查过这个吗? https://dev59.com/-mYr5IYBdhLWcg3wi6vd - dishooom
似乎很不错。我们没有任何安卓API吗? - Moses
我非常确定没有。在我的一个之前的项目中,我不得不深入研究FFmpeg以寻找类似的东西。 - dishooom
好的,谢谢你节省了我的时间。请在答案面板中写下它。我会将其标记为正确的答案,以便对其他人有所帮助。 - Moses
3个回答

5

是的,获取mp3封面艺术是可能的。

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(mPath);
InputStream inputStream = null;
if (mmr.getEmbeddedPicture() != null) {
    inputStream = new ByteArrayInputStream(mmr.getEmbeddedPicture());
 }
mmr.release();

bitmap = BitmapFactory.decodeStream(inputStream);

这行代码是干什么用的?“BitmapFactory.Options options = new BitmapFactory.Options();” - sirvon

4
使用下面的方法,你可以获取图片文件的专辑封面uri。
public Uri getArtUriFromMusicFile(File file) {
        final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        final String[] cursor_cols = { MediaStore.Audio.Media.ALBUM_ID };

        final String where = MediaStore.Audio.Media.IS_MUSIC + "=1 AND " + MediaStore.Audio.Media.DATA + " = '"
                + file.getAbsolutePath() + "'";
        final Cursor cursor = context.getContentResolver().query(uri, cursor_cols, where, null, null);
        Log.d(TAG, "Cursor count:" + cursor.getCount());
        /*
         * If the cusor count is greater than 0 then parse the data and get the art id.
         */
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
            cursor.close();
            return albumArtUri;
        }
        return Uri.EMPTY;
    }

Kartheek,我的文件在网络上,所以我不能使用你的代码。 - Moses
谢谢,但不适用于所有文件:android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: SELECT album_id FROM audio WHERE (is_music=1 AND _data = '/storage/emulated/0/Music/Marked/2013 Gordon & Doyle vs Scooter- Let's Go Biz (Tr-Meet & Yuliana Mash-up).mp3') - user924
我猜是因为文件名中有 ' 符号,你知道怎么修复吗? - user924
path = path.replaceAll("'", "''"); 可以帮助将路径中的单引号替换为两个单引号。 - user924

0

目前在Android中没有直接提供API来从mp3文件中提取专辑封面。您需要通过FFmpeg来提取它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接