android - 驻留在 Android 动态 'on-demand' 功能模块中的原始资源无法作为 URI 引用

标签 android uri android-videoview android-resources dynamic-feature-module

我在将动态模块中的资源作为 URI 引用时遇到问题。

类似这样的:

android.resource://com.redback.dynamic_module/raw/cool_video

似乎无效。

我能够加载动态功能模块并从基本模块实例化一个类,无需任何戏剧性操作,甚至可以使用 AssetFileDescriptor 引用动态模块中的其他原始资源,然后将其提供给像这样的 MediaPlayer:

// Playing a raw sound works fine

val soundRawResId = dynamicModuleResourceProvider.coolSound() // sound file is a raw resource in the dynamic feature module
val asset: AssetFileDescriptor = resources.openRawResourceFd(resId)
mediaPlayer.setDataSource(asset.fileDescriptor, asset.startOffset, asset.length) }
mediaPlayer.prepareAsync() 

// Sound plays!

所以模块和资源似乎加载正常。

但是,我还需要将一些原始资源提供给 VideoView。为此,我需要生成一个 URI:

val videoRawResId = dynamicModuleResourceProvider.coolVideo()

val uri = Uri.Builder()
                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                .authority(resources.getResourcePackageName(resId))
                .appendPath(resources.getResourceTypeName(resId))
                .appendPath(resources.getResourceEntryName(resId))
                .build()

// This builds a uri that looks like this: 

/// `android.resource://com.redback.dynamic_module/raw/cool_video`

// But throws an exception:

// Couldn't open android.resource://com.redback.dynamic_module/raw/cool_video
    java.io.FileNotFoundException: No resource found for: android.resource://com.redback.dynamic_module/raw/cool_video

我尝试对 URI 进行硬编码:

android.resource://com.redback/raw/cool_video

android.resource://dynamic_module/raw/cool_video

但没有效果...

在将资源移动到动态点播功能模块之前,此 VideoView 工作正常,并且如果我使用基本模块中的视频,仍然可以正常工作。这对我来说也许 URI 只需要以不同的方式构建?

目前的解决方法是将资源写入文件并将该文件提供给 VideoView.. 当然,这不太理想,但表明资源已加载并且可访问。

            val name = resources.getResourceEntryName(resId)
            val file = File(context.cacheDir, name)
            if (!file.exists()) {
                val resourceStream = resources.openRawResource(resource.res)
                copyStreamToFile(resourceStream, file)
            }

            setVideoPath(file.absolutePath)

动态模块称为“dynamic_module”,包 ID(在 AndroidManifest 中)与基本模块相同:“com.redback”

最佳答案

Google 开发者关系部门的答复解决了该问题:

val uri = Uri.Builder()
                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                .authority(context.getPackageName()) // Look up the resources in the application with its splits loaded
                .appendPath(resources.getResourceTypeName(resId))
                .appendPath(String.format("%s:%s", resources.getResourcePackageName(resId), resources.getResourceEntryName(resId))) // Look up the dynamic resource in the split namespace.
                .build()

关于android - 驻留在 Android 动态 'on-demand' 功能模块中的原始资源无法作为 URI 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59192240/

相关文章:

android - 调用 setZOrderOnTop(true) 后在 videoView 上绘制

Android RoomDatabase - 无法解析 android.arch.persistence.room :compiler:1. 0.0

iphone - 在正方形 : Why is the image rotated? 上映射纹理

python - libtorrent dht 对等请求?

android - 如何从android 10中的位图获取uri

android - Vitamio for Android的VideoView的 "setVideoHeaders"方法如何添加cookie?

android - TimePickerDialog 和 TimePicker

JavaScript:在 Firefox for Android 上跟踪选择更改

.net - 百分比编码的斜线 ("/") 在请求调度之前被解码

android 通过 gridview 填充所有屏幕