tags:

views:

48

answers:

1

Hi all,

This is a bit unorthodox but I'm trying to figure out if there's a way to access files stored in the src tree of my applications apk in Android. I'm trying to use i-Jetty (Jetty implementation for Android) and rather than use it as a separate application and manually download my war file, I'd rather just bake i-jetty in.

However, in order to use (easily) standard html/jsp I need to be able to give it a document root, preferably within my application's apk file. I know Android specifically works to prevent you from accessing (freely) the stuff on the actual system so this may not be possible, but I'm thinking it might be possible to access something within the apk. One option to work around this would be to have all of the files stored in the res directory and then copy them to the sdcard on startup but this wouldn't allow me to automatically remove the files on uninstall.

To give you an idea of what I've tried, currently, the html files are stored in org.webtext.android

Context rootContext = new Context(server_, "/", Context.SESSIONS);
rootContext.setResourceBase("org/webtext/webapp");

Returns a 404 error.

final URL url = this.getClassLoader().getResource("org/webtext/webapp");
Context html = new WebAppContext(url.toExternalForm(), "/");

Blows up with a NullPointerException because no URL is returned from the getResource call.

Any thoughts would be greatly appreciated!

Thanks,

Chris

Edit In case anybody finds this looking for a similar answer, I never found a great answer. I used a slight hack on what was suggested below. I created a war file with only html/jsp content (servlets I added to the server manually and was able to keep in the in the src tree) and stored it in the assets folder. When the app starts, I copy the war file to the sdcard. When the app closes, I delete the copy. Of course, if the app is killed rather than gracefully exiting, I don't get the chance to delete it, but that's not a huge deal.

+1  A: 

You might consider creating a subclass of Context that supports loading files out of the assets/ directory using AssetManager.

CommonsWare
That's a good idea, would getResource be the method I would need to override with this functionality?
Chris Thompson
From an Android `Context` object (not to be confused with an i-jetty one), call `getAssets()` to get an `AssetManager`.
CommonsWare
Ahh very important clarification lol. Thanks! I'll take a look.
Chris Thompson
So that's a very workable solution, however I'm not sure how to wire that to Jetty such that when a request comes in, that context object can handle the request.
Chris Thompson
I have never used i-jetty (though I used to use regular Jetty a fair bit), so I cannot answer that question. To be perfectly frank, I'm not convinced that i-jetty is the right solution for whatever business problem you are trying to solve.
CommonsWare