如何引用 Android“资产”跨包?

发布于 2024-10-28 20:48:59 字数 657 浏览 5 评论 0原文

我有一个以“免费”和“专业”版本发布的 Android 应用程序。我已经使用从两个版本引用的基础“库”项目设置了我的项目,这样我的包集如下所示:

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

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

有没有办法指定“file:///android_asset/”URI,以便它访问父包中的“assets”目录?

我发现的部分解决方案涉及从“原始”目录读取 HTML 文件,然后将生成的字符串推送到我的 WebView 对象,但是对于纯文本 HTML 页面(例如带有图像、就像我的一样)。

干杯。

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

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.

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

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).

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-11-04 20:48:59

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

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

这似乎并没有那么糟糕。我已经验证这两个包不需要共享用户 ID。

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();

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

花之痕靓丽 2024-11-04 20:48:59

作为一名同时拥有专业版和免费版的开发人员,根据经验,我可以说,不要尝试跨应用程序共享资源。即使你让它发挥作用,你最终也会遇到比它值得的更多的麻烦。

如果您绝对必须这样做,则每个应用程序必须共享相同的系统用户,并且您必须在一个应用程序中创建一个模仿另一个应用程序的上下文。这些并不难做到,但可能会成为维护的噩梦。

As a developer that has both pro and free versions out there I can say from experience, don't try to share assets across applications. Even if you get it to work, you'll end up with more hassle than it's worth.

If you absolutely must do this, each app must share the same system user and you must create a context in one app that mimics the other. These aren't hard to do but and can be a maintenance nightmare.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文