获取 mp3 文件的单个图像太慢

Getting individual image of mp3 file is too slow

我正在尝试在我的 phone 上获取 mp3 文件的图像。折腾了几天终于知道了;

我所有的 mp3 文件都在那里。没关系。现在我想得到他们的封面图片。

在哪里可以找到文件的图像路径? MetadataRetriever class 在哪里可以用 getEmbeddedPicture 得到它?嵌入的图片在哪里?

谢谢。最好的问候

mediaMetadataRetriever.getEmbeddedPicture() 是一种本机方法,每个本机方法调用都有代价,因为 JNI 调用需要时间。所以,如果你重复这样的调用,你可能会浪费很多时间。您可以尝试按需异步调用此方法并缓存结果。尝试结合 glide 这将同时解决你的两个问题,滑行调用自然是 asnyc,所以你不需要实现 AsyncTask class 它也会缓存你的图像。

这是滑行的基本用法:

Glide.with(context).load(mediaMetadataRetriever.getEmbeddedPicture());

我鼓励你阅读 glide documents。它还有很多其他功能。

希望对您有所帮助。

您可以获得此处提到的专辑封面

基本上,

Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
                new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
                MediaStore.Audio.Albums._ID+ "=?", 
                new String[] {String.valueOf(albumId)}, 
                null);

if (cursor.moveToFirst()) {
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    // do whatever you need to do
}

但是要获得个人歌曲艺术,这就是我记得的做法。希望对你有帮助。

float ht_px = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
        float wt_px = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());

        Bitmap artwork, bitmap2 = null;

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        byte[] rawArt;
        BitmapFactory.Options bfo = new BitmapFactory.Options();

        try {
            mmr.setDataSource(songUri);  //songUri is the uri of the song. You need to extract this from the song id
            rawArt = mmr.getEmbeddedPicture();
            if (rawArt != null)
                bitmap2 = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
            if (bitmap2 == null)
                bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.def_icon);
            artwork = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);   //This is the bitmap you need
        } catch (Exception e) {
            //Exception handling
        }
    }