【问题标题】:Retrieving Assets from Android Lib Project从 Android Lib 项目中检索资产
【发布时间】: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?还是有其他方法可以/应该这样做?

【问题讨论】:

    标签: android sql


    【解决方案1】:

    来自docs

    这些工具不支持使用原始资产文件(保存在 assets/ 目录)在库项目中。使用的任何资产资源 应用程序必须存储在 assets/ 目录中 应用项目本身。但是,资源文件保存在 res/ 支持目录。

    你能用 res/raw 和 openRawResource() 代替吗?

    【讨论】:

    • 感谢我能够使用以下新代码使其正常工作:Configuration c = new Configuration(); c.setToDefaults(); InputStream myInput = new Resources(myContext.getAssets(), null, c).openRawResource(R.raw.database); OutputStream myOutput = new FileOutputStream(dbLocation);
    猜你喜欢
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多