如何使用 glide 从 android 中的音频 url 获取缩略图

Posted

技术标签:

【中文标题】如何使用 glide 从 android 中的音频 url 获取缩略图【英文标题】:how to get thumbnail from audio url in android using glide 【发布时间】:2022-01-01 14:33:42 【问题描述】:

尝试使用 glide 从 url 加载音频缩略图但不工作

Glide
                .with(this)
                .load("http://api.boleiachain.com/upload/musictest.mp3")
                .apply(options)
                .centerCrop()
                .placeholder(R.drawable.ic_loading)
                .into(civ_image);

我已经试过了

public  Bitmap coverpicture(String path) 

        Bitmap bitmap=null;
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(path, new HashMap<String, String>());
        try 
            final byte[] coverImage = mediaMetadataRetriever.getEmbeddedPicture();
             bitmap= BitmapFactory.decodeByteArray(coverImage, 0, coverImage.length);
         catch (Exception e) 
            e.printStackTrace();
        
        return bitmap;

    

Glide
                .with(this)
                .load(coverpicture("http://api.boleiachain.com/upload/musictest.mp3"))
                .apply(options)
                .centerCrop()
                .placeholder(R.drawable.ic_loading)
                .into(civ_image);

它可以工作但有延迟,这在 recyclerView 适配器中不是一个好习惯

【问题讨论】:

无论如何,网络通话都需要时间, 【参考方案1】:

您可以在调用coverPicture方法之前缓存位图并检查缓存是否存在

  ....load(getCoverpicture("http://api.boleiachain.com/upload/musictest.mp3"))



HashMap<String,Bitmap> cache = new HashMap<String,Bitmap>();
public  Bitmap getCoverpicture(String path) 
    if(cache.containsKey(path)) return cache.get(path);
    else
        Bitmap bitmap = coverpicture(path);
        cache.put(path,bitmap);
        return bitmap;
    

它有一些语法错误

【讨论】:

以上是关于如何使用 glide 从 android 中的音频 url 获取缩略图的主要内容,如果未能解决你的问题,请参考以下文章