2

我在将动态模块中的资源作为 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”

4

1 回答 1

1

谷歌开发者关系的回答解决了这个问题:

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()
于 2020-04-09T07:17:28.440 回答