如何访问资源的一款Android库项目 [英] how to access resources in a android library project
问题描述
我建立一个机器人库项目,需要一定的静态资源(图片/ XML等)内部。
I am building an android library project which need some static resources(images/xml and etc) internally.
然后,我不知道在那里我可以把这些资源,以及如何访问它们?
Then I wonder where I can put these resources and how to access them?
由于我把资源中的资产
文件夹。我用的是AssetManager访问资源:
Since I put the resources in the assets
folder. And I use the AssetManager to access the resources:
public class DataProvider {
byte[] getDrawableData(int num) {
AssetManager am = Resources.getSystem().getAssets();
InputStream is = null;
try {
is = am.open("t.jpg");
} catch (IOException e) {
return "".getBytes();
}
if (is != null) {
Drawable dw = Drawable.createFromStream(is, null);
Bitmap bm = ((BitmapDrawable) dw).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
return "".getBytes();
}
}
不过,我得到了错误: W / System.err的(19583):java.io.FileNotFoundException:t.jpg
这是什么问题?
由于桑卡尔V
表示,库项目不支持原始资产。然后,看来我必须把resurces下的 RES
文件夹。
As Sankar V
said the library project does not support the raw assets. Then It seems that I have to put the resurces under the res
folder.
不过,我曾尝试使用了 Resources.getSystem()。getResources()......
方法不能得到我想要的。而人们说,这种方法只能获得系统级资源,而不是应用程序级别的资源。
But I have tried to use the Resources.getSystem().getResources()....
method which can not get what I want. And people said that this method can only get the system level resources rather than the application level resources.
如果这是真理。这是否意味着我必须持有上下文
对象的引用的任何地方我想获得任何资源?
If this is the truth. Does it mean that I have to hold a reference of the Context
object anywhere I want to get any resources?
推荐答案
然后,我不知道在那里我可以把这些资源,以及如何访问它们?
从Android电子文档的
From Android Doc's
库项目不能包含原始资产
该工具不支持在库项目中使用的原材料资源文件(保存在资产/目录)。由应用程序使用任何资产资源必须被存储在应用项目本身的资产/目录。然而,保存在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.
文档参考链接: LibraryProjects
解决方法: 如何访问保存在资源的资源文件/应用程序项目库项目<目录/一>
Solution : How to access resource files saved in res/ directory of library project from application project
注意:如果您要访问的库项目本身的资源,增加资源,以 RES /目录
不是资产
。由于资产的文件不会被捆绑在.jar文件。
您可以使用产生的参考R.java
文件访问它像往常一样。
Note : If you want to access the resources from Library Project itself add the resources to res/ directory
not in assets
. Because the files in assets won't be bundled with the .jar file.
You can access it as usual using the reference generated in R.java
file.
例如:要绘制使用访问图像
R.drawable.image_name
这是否意味着我必须保持上下文对象的引用的任何地方我想获得任何资源?
真的,你应该保持一个对上下文访问资源
Really you should hold a reference to context to access the resources
更新:
现在的Android库项目可以生成AAR(安卓Archive)文件,它允许你捆绑所需的库项目,而不是老的jar(Java归档)文件,该文件只允许你捆绑Java类的资源文件。
Now Android library project can be generated as aar (Android Archive) file which allows you to bundle the resource files needed for the library project instead of the old jar (Java Archive) file which only allow you to bundle Java classes.
这篇关于如何访问资源的一款Android库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!