[英]Raw Resource residing in an Android Dynamic 'on-demand' Feature Module can't be referenced as a URI
我在將動態模塊中的資源作為 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 Developer Relations 的回答解決了這個問題:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.