如何在Android(kotlin)中访问资产打包数据 [英] How to access assets-pack data in Android (kotlin)
问题描述
我使用资产打包库在Android kotlin应用程序中打包了一些资产:
现在根据文档( https://developer.android.com/guide/playcore/asset-delivery/integrate-java ),我可以像这样访问资产:
import android.content.res.AssetManager;...上下文context = createPackageContext("com.example.app",0);AssetManager assetManager = context.getAssets();InputStream是= assetManager.open("asset-name");
我将软件包名称更改为我的软件包名称.但是资产名称是什么?!不是我的 models
资产文件夹中文件的名称.我试过了我想到的是 assetManager.list(p)
和 p
,但是我找不到资产的存储位置.
对于 p =''
, assetManager.list(p)
的结果为
<代码> hw_pc_white_apps.xmlhw_pc_white_apps_pad.xml图片Permission_grant_policy.xmlPermission_grant_policy_oversea.xmlPFWsfpconfig.jsonukeyapp.xml水碎片水网页套件wifi_policy.xmlwifipro_regexlist.xml
这对我也没有意义.如何从资产包访问资产?
好,我找到了解决方案:Android Studio默认不提供资产包,需要在 Run->下调整配置.编辑配置->从应用程序包中交付APK
( https://developer.android.com/指南/应用程序捆绑/测试)以交付资产和应用程序.
现在可以像主应用程序的资产一样访问这些资产.
I packed some assets in my Android kotlin app using the asset-pack library: https://developer.android.com/guide/app-bundle/asset-delivery/build-native-java
Not I should be able to access the assets using the AssetManager
via context.assets
. However, I have no clue what the correct path is.
My assets pack is called models
and correctly created in the root folder of the .aab
file, so I guess it is also installed correctly.
Now according to the documentation (https://developer.android.com/guide/playcore/asset-delivery/integrate-java) I can access the assets like this:
import android.content.res.AssetManager;
...
Context context = createPackageContext("com.example.app", 0);
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open("asset-name");
where I chane the package name to my package name. But what is the asset name?!? It's not the name of the files in my models
asset folder. I tried
assetManager.list(p)
with p
as anything I could think of, but I could not find where my assets are stored.
For p=''
the result of assetManager.list(p)
is
hw_pc_white_apps.xml
hw_pc_white_apps_pad.xml
images
permission_grant_policy.xml
permission_grant_policy_oversea.xml
pfw
sfpconfig.json
ukeyapp.xml
water.frag
water.vert
webkit
wifi_policy.xml
wifipro_regexlist.xml
which also makes no sense to me. How can I access assets from asset packs?
Ok, I found the solution: the Android Studio does not deliver the asset packs by default, the configuration needs to be adjusted under Run -> Edit configuration -> Deliver APK from app bundle
(https://developer.android.com/guide/app-bundle/test) to deliver assets and the app.
Now the assets are accessible as if they were assets of the main app.
这篇关于如何在Android(kotlin)中访问资产打包数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!