Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

how to access resources in a android library project

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?

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();
    }
}

However I got the error: W/System.err(19583): java.io.FileNotFoundException: t.jpg

What is the problem?


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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Then I wonder where I can put these resources and how to access them?

From Android Doc's

Library projects cannot include raw assets

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.

Docs Reference Link : LibraryProjects

Solution : How to access resource files saved in res/ directory of library project from application project

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.

Ex: To access a image from drawable use

R.drawable.image_name

Does it mean that I have to hold a reference of the Context object anywhere I want to get any resources?

Really you should hold a reference to context to access the resources

Update:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...