1

I have images placed in following path

res>drawable>Images>Currencies>[All images]

I have made a custom spinner Adapter.

viewHolder.flag = (ImageView) view.findViewById(R.id.UICurrencyCurrencyFlag);
            viewHolder.flag.setImageDrawable(drawable);

How can i set these images in my ImageView. Images format is png

Best Regards

Rajkiran
  • 15,845
  • 24
  • 74
  • 114
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

3 Answers3

1

You have 2 choices:

  1. Put all images in drawable folders without subfolders.
  2. Put all images in asset with your folder structures.

The differences are:

  1. If you go for approach 1, you can retrieve the resource by id. (R.drawable.*)
  2. If you go for approach 2, you get to maintain your folders, but you need to read the resource as raw.

To open file in asset folder, you can refer to a previous post: Opening a File from assets folder in android

Suggestion:

Rename your file as images_currency_image1.jpg to avoid subfolders but keep unique-ness. I am not sure if this help in your situation.

Community
  • 1
  • 1
Calvin
  • 3,302
  • 2
  • 32
  • 41
  • okay they are currency flags like PKR.PNG USD.PNG Etc. i have USD saved in a string. so i can just add .png to create filename. Now R.drawable.* seems useless because i can't append filename i think. If i use asset folder then what should i do? – Muhammad Umar Mar 22 '12 at 05:53
  • I added a reference link for you. – Calvin Mar 22 '12 at 05:56
0

You don't really need to make subfolders inside drawable folder. And even if you do, you can't access the contents inside it. And it doesn't even make sense. Why you need subfolders when android is taking care of giving you the drawables by their id. You place your images directly into res/drawable folder like res/drawable/[All images]. And then try to access by R.drawable.UICurrencyCurrencyFlag and not R.id.UICurrencyCurrencyFlag. All the best.

Rajkiran
  • 15,845
  • 24
  • 74
  • 114
  • i need to place them in another folder, i can put them in asset folder too if that is easy? – Muhammad Umar Mar 22 '12 at 05:45
  • No. You can't make any subfolder inside drawable. Or even if you do, you can't access the files inside it. Check my updated answer. – Rajkiran Mar 22 '12 at 05:55
0

if u place in assets folder u can access in following manner
InputStream bitmap;
try {
bitmap = getAssets().open("icon.png");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
ImageView img = (ImageView) findViewById(R.id.myId);
img.setImageBitmap(bit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

sush
  • 476
  • 1
  • 7
  • 20