驻留在Android动态系统中的原始资源';按需';功能模块can';不能作为URI引用
作为URI引用动态模块中的资源时遇到问题 大概是这样的:驻留在Android动态系统中的原始资源';按需';功能模块can';不能作为URI引用,android,uri,android-videoview,android-resources,dynamic-feature-module,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 = d
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!
因此,模块和资源似乎加载正常
但是,我还需要将一些原始资源输入到视频视图中。为此,我需要生成一个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需要以不同的方式构造
目前的解决方法是将资源写入一个文件,并将该文件交给视频视图。。这当然不太理想,但表明资源已加载且可访问
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()