Reputation: 792
I have an android application in which I have several images in assets folder. Now I want to make an array of that images. Now my problem is :- when our images are in drawable we can make an array like
int x[] = {
R.drawable.ss,
R.drawable.aa, R.drawable.sk,
R.drawable.xx
};
and so on. how can i make an array of images same as above when my images are in assets folder. I want to make an array at class level.
Upvotes: 18
Views: 30107
Reputation: 5368
You have to read image by image like below:
You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap.
private Bitmap getBitmapFromAsset(String strName)
{
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
Upvotes: 28
Reputation: 7092
If your images are stored in image folder in assets directory then you can get the list of image as way
private List<String> getImage(Context context) throws IOException {
AssetManager assetManager = context.getAssets();
String[] files = assetManager.list("image");
List<String> it = Arrays.asList(files);
return it;
}
Upvotes: 6
Reputation: 6434
// load image from asset folder
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
or you can create drawable array
Drawable d []={d};
Upvotes: 4
Reputation: 1377
You have wrong meaning about drawables and assests. You can make array od "drawables", because all drawables have own ids in R (like R.dawable.ss), so you can use specified integer to get drawable if you have proper context.
Other way managing files like images is assests. If you want do manage images by theirs ids you must add this images do drawables. In other way, assests files must be managed like a simple files in dir.
You must get files from assets AssetManager am=this.getAssets();
and then prepare file to read/write. If you have images you can do something like this:
try {
Bitmap bmp=BitmapFactory.decodeStream(am.open("009.gif"));
imageView.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 1