Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference Android 'assets' across packages?

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.

like image 691
foofaa Avatar asked Mar 30 '11 22:03

foofaa


1 Answers

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.

like image 133
mhsmith Avatar answered Oct 17 '22 05:10

mhsmith