1

how to get album art (i.e image file ) from a song file , i am able to get the album art of a file which is in music folder , but how to get ,when the music file is in different path other than music folder

Please any suggestion . Thanks in advance

4

2 回答 2

5

使用此代码:)

private Bitmap getAlbumImage(String path) {
    android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(path);
    byte[] data = mmr.getEmbeddedPicture();
    if (data != null) return BitmapFactory.decodeByteArray(data, 0, data.length);
    return null;
}
于 2019-01-12T07:28:04.477 回答
0

首先从媒体商店获取所有歌曲的列表。

public void getSongList() {
    // retrieve song info

    ContentResolver res = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor cursor = res.query(musicUri, null, null, null,
            null);


    if (cursor != null && cursor.moveToFirst()) {
        // get columns
        int titleColumn = cursor.getColumnIndex(MediaColumns.TITLE);
        int idColumn = cursor.getColumnIndex(BaseColumns._ID);
        int artistColumn = cursor.getColumnIndex(AudioColumns.ARTIST);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

        // add songs to list
        do {
            long thisId = cursor.getLong(idColumn);
            String pathId = cursor.getString(column_index);
            Log.d(this.getClass().getName(), "path id=" + pathId);

            metaRetriver.setDataSource(pathId);
            try { 
                art = metaRetriver.getEmbeddedPicture();
                Options opt = new Options();
                opt.inSampleSize = 2;
                songImage = BitmapFactory .decodeByteArray(art, 0, art.length,opt);
            }
            catch (Exception e) 
            { imgAlbumArt.setBackgroundColor(Color.GRAY); 
            }

            String thisTitle = cursor.getString(titleColumn);
            String thisArtist = cursor.getString(artistColumn);
            songList.add(new Song(thisId, thisTitle, thisArtist,songImage));
        } while (cursor.moveToNext());

    }

然后在获得歌曲列表后,您可以使用song.getsongImage();

Bitmap bm= BitmapFactory.decodeFile(song.getsongImage());
    ImageView image=(ImageView)findViewById(song.getsongImage());
于 2017-01-20T09:29:55.020 回答