如何跨包引用Android的“资产"? [英] How to reference Android 'assets' across packages?

查看:104
本文介绍了如何跨包引用Android的“资产"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以免费"和专业版"发布的Android应用.我已经用两个版本都引用的基本库"项目设置了我的项目,使得我的软件包集看起来像这样:

I have an Android App that is published in a 'free' and 'pro' version. I have set up my project with a base 'library' project that is referenced from both versions, such that my package set looks like this:

  • com.example.myapp
  • com.example.myapp.free
  • com.example.myapp.pro

我的基础库"项目中的Activity类之一将帮助文件加载到WebView中:WebView.loadUrl("file:///android_asset/help.html").此类在免费"和专业"版本中都进行了扩展(出于超出此问题范围的原因),但是我希望这两个版本都引用相同的HTML文件(即父程序包中的HTML文件).但是,在我当前的设置下,需要将HTML文件复制到"file://"的"com.example.myapp.free"和"com.example.myapp.pro"包下的"assets"文件夹中. /android_asset/可以使用的URI.

One of the Activity classes in my base 'library' project loads a help file into a WebView: WebView.loadUrl("file:///android_asset/help.html"). This class is extended in both the 'free' and 'pro' versions (for reasons outside the scope of this question), but I'd like both versions to reference the same HTML file (i.e. the one in the parent package). Under my current set up, however, the HTML file needs to be duplicated in the 'assets' folder under the 'com.example.myapp.free' and 'com.example.myapp.pro' packages for the "file:///android_asset/" URI to work.

是否可以通过指定"file:///android_asset/" URI的方式来访问父包中的"assets"目录?

Is there a way to specify the "file:///android_asset/" URI such that it accesses the 'assets' directory in the parent package?

我发现的部分解决方案包括从原始"目录中读取HTML文件,然后将结果字符串推送到我的WebView对象,但是除了纯文本HTML页面(例如,图片,就像我的图片一样.)

A partial solution I found involves reading the HTML file from the 'raw' directory and then pushing the resulting string to my WebView object, but this would be messy to do for anything more than a text-only HTML page (e.g. one with images, like my one).

干杯.

推荐答案

由于安德鲁·怀特(Andrew White)没有解释如何针对他的建议,我将向您展示:

Since Andrew White didn't explain how to do what he recommends against, I'll show you:

Context otherContext = context.createPackageContext("other.package.name", 0);
AssetManager am = otherContext.getAssets();

这看起来还不错.而且我已经证实,这两个软件包无需共享用户ID.

That doesn't seem so bad. And I've verified that there's no need for the two packages to share a user ID.

这篇关于如何跨包引用Android的“资产"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆