1

I need to set a list of images in ViewPager. I have a list of images in my asset folder. I grouped the images in seperate folders inside asset like assets/images; assets/images1,...... Now I need to get the id of the images (say images1 alone) in int[] array. I got the name of the images(images1) in String[] array. How to get the id of the images in string[] using their names.

Priya Raja
  • 71
  • 1
  • 7

5 Answers5

1

You have to put your images in drawable folder to access them. Then you can use this code to get the id of images from their name..

 String yourImageName;
 int resID = getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");
Vikram Singh
  • 1,420
  • 14
  • 19
0

Create a folder named "drawable" in your project's "res/".

Say you have Images like ...

res/drawable/image,image1,image2,...

In your ACTIVITY, call them by using R.drawable.image,R.drawable.image1,....

"Now I need to get the id of the images (say images1 alone) in int[] array"

You can also store them in Array and also call them by using the "R.drawable.YOUR_IMAGE_NAME" as,

int[] images = {R.drawable.image,R.drawable.image1,R.drawable.image2,R.drawable.image3,....};

Hope this helps.

sai
  • 548
  • 10
  • 30
  • Sorry Sir, I have nearly 100s of images in my folder. – Priya Raja Sep 19 '13 at 07:56
  • Ok , so you can call them directly when needed using "R.drawable.image_name" right? And if you need to call them using an array also, you have to call that "many" times in an array! – sai Sep 19 '13 at 08:04
  • What is your exact intention of using ASSETS folder. To be clear(of what I know), Assets folder in the ANDROID directory structure is used to store some static files such as JSON,Text Files etc., which we can do using InputStreams , Such "getAssets().open("NEW_FILE.txt");" ... – sai Sep 19 '13 at 08:07
  • Also Check this Link, [get array of ID image when have array of name image in assets folder](http://stackoverflow.com/questions/16305979/how-to-get-array-of-id-image-when-have-array-of-name-image-in-assets-folder?rq=1) Here you can refer your images Names through list. – sai Sep 19 '13 at 08:10
0
int[] array_images = {
                      R.drawable.image0,
                      R.drawable.image1,
                      R.drawable.image2,
                      R.drawable.image3,
                      R.drawable........,
                      ....
                      };
Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

Usually "id" is created only for the images in the resource folder but not for the ones in the asset folder.

Priya Raja
  • 71
  • 1
  • 7
-1

if you are keeping your image in assets than you have to decode it using asset manager .

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}

private void init() {
    InputStream is = null;
    ImageView imageView = new ImageView(this);
    ((RelativeLayout)findViewById(R.id.parent)).addView(imageView);
    try {
     is = getAssets().open("ic_launcher.png");//name of image in your asset folder
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = BitmapFactory.decodeStream(is);
    imageView.setImageBitmap(bitmap);

         // if you dont want bitmap  you can use drawable

       // Drawable d = Drawable.createFromStream(is, null);
    //imageView.setImageDrawable(d);

}   

}

else keep your images in drawable folder in res and use it using R.drawable.imagename

Ishan
  • 1,172
  • 10
  • 25