16

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?

hguser
  • 35,079
  • 54
  • 159
  • 293
  • did you checked the link completely which i provided as a solution. That says "Yes you can if you know the package name of your library" and there is an working solution. – Sankar V Apr 17 '13 at 07:20
  • I read the post. But I can not access the `context` object, so I cannot call the `getResources()....`. – hguser Apr 17 '13 at 07:25
  • I would like to confirm where the class DataProvider resides? in Library project or in application project – Sankar V Apr 17 '13 at 08:31
  • The class `DataProvider` reside in the Library project. – hguser Apr 18 '13 at 02:28
  • 1
    From library project you can access the resource files as usual. For ex you can access a image stored in drawable as `R.drawable.image_name` – Sankar V Apr 18 '13 at 04:56
  • But there is no `context` referenced inside the `DataProvider` class, then I can not use the `getResources()` method. – hguser Apr 18 '13 at 05:07
  • I did mention about the files stored in res/ directory not in assets directory – Sankar V Apr 18 '13 at 05:10

3 Answers3

9

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.

Community
  • 1
  • 1
Sankar V
  • 4,794
  • 3
  • 38
  • 56
  • Sankar: You say that `Library projects cannot include raw assets`. But I have used some third-part library who package the classes and the assets folder together. What is the problem? – hguser Apr 17 '13 at 07:02
  • may i know what that third-party library is? – Sankar V Apr 17 '13 at 07:08
  • I am afraid you misunderstand me. The point is that I am not crying to access the images inside the `Activity` (the context) scope,so I do not use the `getResource(R.drawable.image_name)`. – hguser Apr 18 '13 at 06:48
  • I do understand now. Really you shouldn't access the resources without `Context` because it is the handle to the resources. – Sankar V Apr 18 '13 at 07:02
  • Does it mean that I have to bind a `context` object anywhere I want to access the resources? How about use the `Class.xx.getResources()`? – hguser Apr 18 '13 at 07:26
  • AFAIK Really you should hold a reference to `context` to access the resources. I think it is off the topic to discuss it in more detail here. If you think you can I suggest you to create a new question with related details. – Sankar V Apr 18 '13 at 07:36
  • what if there were some files, like css or js file (whose path will be pointed in the html file to be loaded into webview) in asset folder of the library project, when running the hosting app it could not load the file? – lannyf Nov 30 '18 at 14:00
0

Or you can use a Linked Folder in Eclipse, and link your assets folder across to your new project.

Moz
  • 1,494
  • 6
  • 21
  • 32
  • this helps maybe in eclipse and not in Android Studio which should be prefered now a days. But i don't believe this will help in eclipse too as linked folders ins't linking when project is build – Deniz Celebi Jan 03 '16 at 22:35
0

Okay, its been a while since a working solution has been given to this question. I have been working with creating Android library for few days, and encountered this problem myself as well. I tried all the above solutions put forward, but none of them worked for me.

Recently I watched a video on Android library, and got a glimpse of the solution, even though it was not mentioned that it is necessary for using res directory resources inside library code.

So the solution is simple. Simply, you can check if following line

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourpackage">  <!-- this line -->

</manifest>

is present in your manifest. Otherwise you won't be able to use any of the strings, styles, drawables in your library java files/xml files.

I have checked this on my end, and found this solution randomly, after searching online for nearly 2 days. Try it and tell me if it works.

Divya Gupta
  • 494
  • 8
  • 24