gpt4 book ai didi

android - 从 Android Lib 项目中获取资源

转载 作者:行者123 更新时间:2023-11-29 00:44:35 24 4
gpt4 key购买 nike

我有两个独立的项目,一个增强版和免费版。这两个项目都使用了大量共享代码,因此我试图将共享的所有内容集成到一个 Android 库项目中。

其中一个共享资源是我位于 FreeProject/assests/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?或者还有其他方法可以/应该这样做吗?

最佳答案

来自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.

你可以使用 res/raw 和 openRawResource()相反?

关于android - 从 Android Lib 项目中获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7150509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com