views:

337

answers:

1

I want to know how I can use ResourceBundle.getBundle() in an Android application given that I use it in my Java applications. For example:

I have a properties file, "MyOrg.properties", which I've included in a JAR file named "MyOrg.jar". The path information in the JAR file associated with "MyOrg.jar" is "myorg\" (this is on a Windows system). I build my Java application using "myorg.jar" and note that the following line of code works as expected, which is that it loads the file "MyOrg.properties" from "MyOrg.jar" as a java.util.ResourceBundle:

        ResourceBundle resources = ResourceBundle.getBundle( "myorg.MyOrg" );

Next, I place a modified copy of the file "MyOrg.properties" on the file system in the directory "c:\myorg", which is on my Java application's class path. I now rerun my Java application and note that the Resource.getBundle() returns, as expected, a bundle for the modified copy of "MyOrg.properties" (the one that is on the file system).

The behavior I've just described is what I would like to be able to accomplish with an Android application. However, ResourceBundle.getBundle() throws a MissingResourceException; it fails to find "MyOrg.properties" in either the JAR file or as a stand-alone file.

So I have two questions:

1) - Is it possible for an Android application to retrieve a resource from a JAR file using ResourceBundle.getBundle() (or any other way for that matter)?

2) - Is it possible for an Android application to retrieve a resource from a file using ResourceBundle.getBundle()?

Regarding 2): I'm using the nbandroid plugin with NetBeans 6.7.1 and I've placed copies of "MyOrg.properties" on the file system as follows prior to building my Android application:

MyProject

-- build

-- classes

  myorg (directory contains "MyOrg.properties")

... src

  myorg (directory contains "MyOrg.properties")
+1  A: 

you need to make sure the properties file makes it to the .apk file. your best bet is probably in res/raw/ or assets/. see also PropertyResourceBundle's constructors (since it's easy to get hold of an InputStream).

Elliott Hughes