如何引用的资产在库项目资产、项目

2023-09-12 11:14:25 作者:日青良月

在属于库的项目,我叫一个类:

In a class belonging to a Library project I call:

webview.loadUrl("file:///android_asset/info.html", null);

不幸的是,这只是工作,如果我的复制文件info.html里进的应用程序的的项目资产的文件夹中。

Unfortunately, this only works if I duplicate the file info.html into the Application's project asset folder as well.

有没有办法告诉一个Android库code:看在图书馆的资源文件夹中文件,不可以在应用程序的资产文件夹

Is there a way to tell an Android library code: "look for this file in the library's assets folder, not in the application's assets folder" ?

推荐答案

从 Android的文档:

库项目不能包含原始资产

该工具不支持在库项目中使用的原材料资源文件(保存在资产/目录)。由应用程序使用任何资产资源必须被存储在应用项目本身的资产/目录。然而,保存在res /目录资源文件都支持。

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.

如果你想包括一个图书馆项目文件,你需要把它的资源而不是资产。如果你想从你的库项目加载HTML文件到一个web视图,这意味着你必须去比通常的资产URL更迂回的方法。相反,你将不得不读取资源数据,并使用类似loadData.

If you want to include files from a Library project, you'll need to put it in the resources instead of the assets. If you're trying to load HTML files from your library project into a WebView, this means that you'll have to go a more roundabout method than the usual asset URL. Instead you'll have to read the resource data and use something like loadData.