2

I have two separate projects, a plus and free version. Both projects use a considerable amount of shared code, so I am attempting to integrate everything that is shared into a Android Library Project.

One of the shared resources is a database that I had sitting in FreeProject/assests/database.db. This file is shared between both projects and I would like to move it to LibraryProject/assets/database.db.

In the code I was moving the database to the database folder with the following:

String dbLocation = "/data/data/" + myContext.getPackageName() + "/databases/" + DBNAME;
//Reading the asset and writing it to the database folder.
InputStream myInput = myContext.getAssets().open("database.db");
OutputStream myOutput = new FileOutputStream(dbLocation);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

So my question is, how can I call getAssets() to get an AssetManager for the library project? Or is there another way that I could/should be doing this?

Matt Garriott
  • 362
  • 2
  • 12

1 Answers1

2

From the docs:

The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.

Could you use res/raw and openRawResource() instead?

Alec B. Plumb
  • 2,490
  • 25
  • 25
  • Thanks I was able to get it working using the following new code: `Configuration c = new Configuration(); c.setToDefaults(); InputStream myInput = new Resources(myContext.getAssets(), null, c).openRawResource(R.raw.database); OutputStream myOutput = new FileOutputStream(dbLocation);` – Matt Garriott Aug 22 '11 at 17:10