Flutter中audio_service包在Android Auto无法显示歌曲封面求助
Android Auto 中音频封面无法显示的解决办法
针对你遇到的audio_service在Android Auto中无法显示歌曲封面的问题,以下是具体的排查和解决步骤:
私有缓存目录无法被Android Auto访问
Android Auto运行在独立进程,无法直接读取你的APP私有缓存目录(/data/user/0/你的包名/cache/)内的文件。即便使用内容URI,也要确保配置正确并授予访问权限:- 在
AndroidManifest.xml中配置FileProvider:<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> - 在
res/xml/下创建file_paths.xml,指定可访问的目录:<paths xmlns:android="http://schemas.android.com/apk/res/android"> <cache-path name="cache_images" path="." /> </paths> - 生成URI并给Android Auto关联进程授予临时读取权限:
final file = File("/data/user/0/你的包名/cache/8744.png"); final uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file); // 给Google服务进程授予权限,Android Auto依赖此进程 context.grantUriPermission( "com.google.android.gms", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION ); MediaItem.add( id: song.id, title: song.title, artUri: uri, );
- 在
检查图片规格是否符合要求
Android Auto对封面图片有明确要求:- 格式优先选择JPG/PNG
- 最小尺寸400x400像素,推荐800x800像素
- 文件大小尽量控制在1MB以内,避免加载超时
确保媒体元数据主动更新
更新MediaItem后,调用AudioServiceBackground.setMediaItem(mediaItem)主动通知音频服务客户端,确保Android Auto能接收到封面更新事件。验证内容URI的可访问性
如果使用自定义内容URI,先在手机端通过浏览器打开该URI确认能正常加载图片。也可以尝试将图片移至公共存储目录(如Pictures文件夹),再生成内容URI测试是否能在Android Auto中显示。无需额外的Android Auto专属权限
Android Auto不需要单独申请权限,只要APP拥有READ_EXTERNAL_STORAGE(访问公共存储时)或正确配置FileProvider,即可满足封面读取需求。
内容的提问来源于stack exchange,提问作者Ruchit Soni
相关产品推荐
相关产品推荐

