1

I have an expandableListView in my activity and passing data from JSON that I get from server. Everything is working fine Except the image part.
I'm getting image path as string within JSON for different sizes (48x48, 72x72, 96x96 etc.) but unable to figure out how to assign image according to the screen density. I think I can Add these images into the corresponding drawable folder and android would do the rest. But on searching I did not find any example on how to add images in drawable folder dynamically.

If there is any way to add images dynamically into drawable folder please let me know OR suggest any other appropriate solution.

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69

3 Answers3

2

You can't add images dynamically into the drawable folders.

But you can get the device's screen density programmatically. You can use that value in your api and return appropriate image(s).

Myat Min Soe
  • 787
  • 8
  • 32
0

You must understand whats drawables in android by this link

and you must know that you can't put any images into them dynamically, icons and static images must be into drawables folders, for other images which you are using them into application you can get image and thumbnail

DolDurma
  • 15,753
  • 51
  • 198
  • 377
0

it's not possible to save in drawable at runtime. you can't use assets folder too.

if I were you it I didn't bother my self to determine screen density. use Glide and Picasso in order to load image asynchronously.

Update :
the thing that i should mention is that, if the ImaveView is a fixed size view by dp it'll appear "the same size" on all devices as per Android's specs. glide and Picasso read the view size by pixel and then scale the image to that size.

I prefer Glide because it has a smaller memory footprint by making some different assumptions like caching the resized images instead of the full-sized images.

Edit:
@greenapps you are completely right. it cannot be saved in assets too. try to create a directory and save drawable into that

M.Armoun
  • 1,075
  • 3
  • 12
  • 38
  • I'm using [https://amitshekhariitbhu.github.io/Fast-Android-Networking/loading_image_into_imageview.html#loading-image-from-network-into-imageview] that provide similar feature too but its not good to load same size image for all screen density. – Inzimam Tariq IT Oct 31 '17 at 10:22
  • Do these libraries scale image according to density automatically? – Inzimam Tariq IT Oct 31 '17 at 10:27
  • `it's not possible to save in drawable at runtime. you can use asset folder instead.` No. You cant either. Read only too. – greenapps Oct 31 '17 at 15:24