You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安卓音乐应用滚动展示歌曲图片时崩溃问题求助

问题定位与解决方案

崩溃原因分析

从崩溃日志可明确:崩溃触发于SongAdapter.java:128的retriever.setDataSource(uri),抛出java.lang.IllegalArgumentException。核心原因是传入的歌曲路径无效、为空,或对应文件已删除但仍存在于列表中,导致MediaMetadataRetriever无法正常加载数据源。

另外,当前代码直接在RecyclerView的onBindViewHolder(主线程)中调用MediaMetadataRetriever解析专辑图,不仅会造成列表滚动卡顿,还会因同步IO操作放大异常影响范围。

修复方案

1. 为getAlbumart方法添加参数校验与异常捕获

在解析专辑图的方法中直接处理无效路径和异常,避免崩溃扩散:

private byte[] getAlbumart(String uri) {
    // 先校验路径有效性
    if (uri == null || uri.isEmpty()) {
        return null;
    }
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(uri);
        return retriever.getEmbeddedPicture();
    } catch (IllegalArgumentException e) {
        // 捕获无效路径异常
        e.printStackTrace();
        return null;
    } finally {
        // 确保资源释放,避免内存泄漏
        try {
            retriever.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. 优化专辑图加载逻辑,避免主线程阻塞

推荐将专辑图解析交给Glide处理(Glide支持直接从音频文件路径提取专辑图),自动完成线程调度、缓存和异常处理,简化代码同时提升性能:

// 替换原有的专辑图加载逻辑
String songPath = msong.get(position).getPath();
if (songPath != null && !songPath.isEmpty()) {
    Glide.with(mcontex)
         .asBitmap()
         .load(new File(songPath))
         .error(R.drawable.lastlogo) // 加载失败时显示默认图
         .into(holder.albumpic);
} else {
    Glide.with(mcontex).load(R.drawable.lastlogo).into(holder.albumpic);
}

3. 修复删除逻辑的潜在问题

删除歌曲后,需确保后续不会访问到已删除的文件路径。可在onBindViewHolder中添加文件存在性校验:

// 在加载数据前校验文件是否存在
Song song = msong.get(position);
String songPath = song.getPath();
File songFile = new File(songPath);
if (!songFile.exists()) {
    // 文件不存在则移除列表条目
    msong.remove(position);
    notifyItemRemoved(position);
    return;
}
// 后续的标题、艺术家、专辑图加载逻辑...

额外优化建议

  • 初始化歌曲列表时,过滤掉无效或不存在的音频文件
  • 为专辑图添加内存/磁盘缓存,避免重复解析
  • 对RecyclerView实现分页加载,降低一次性加载大量数据的内存压力

内容的提问来源于stack exchange,提问作者Morshed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:06:22