I am trying to load an image from the asset
folder and then set it to an ImageView
. I know it's much better if I use the R.id.*
for this, but the premise is I don't know the id of the image. Basically, I'm trying to dynamically load the image via its filename.
For example, I randomly retrieve an element in the database
representing let's say a 'cow', now what my application would do is to display an image of a 'cow' via the ImageView
. This is also true for all element in the database
. (The assumption is, for every element there is an equivalent image)
thanks in advance.
EDIT
forgot the question, how do I load the image from the asset
folder?
Checkout this code . IN this tutorial you can find how to load image from asset folder.
// load image
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); ims .close(); } catch(IOException ex) { return; }
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