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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…