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?
You can find your History, Watch later, Playlists, and other channel details in your Library. To find your Library, go to the bottom menu bar and select Library .
The android default libraries like appcompact, design support libraries are stored in your folder where you have installed the SDK, precisely <SDK FOLDER>/extras/android/m2repository/com/android . The 3rd party libraries are stored in . gradle/caches/modules-2/files-2.1 folder.
The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With