【发布时间】:2011-08-22 16:27:45
【问题描述】:
我有两个独立的项目,一个加号版和免费版。这两个项目都使用了大量的共享代码,因此我尝试将所有共享的内容集成到一个 Android 库项目中。
其中一个共享资源是我在 FreeProject/assets/database.db 中的一个数据库。此文件在两个项目之间共享,我想将其移至 LibraryProject/assets/database.db。
在代码中,我使用以下内容将数据库移动到数据库文件夹:
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();
所以我的问题是,如何调用 getAssets() 来获取库项目的 AssetManager?还是有其他方法可以/应该这样做?
【问题讨论】: