2

I am using the below code to get 9 (bird1 to bird9) images from drawable folder by manually giving each name.

How can i use an array or replace R.drawable.bird1 with variable ( as the below statement accept only actual value) to get all the 9 images in an array?

Drawable myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird1, null);
        bitmap1 = ((BitmapDrawable) myDrawable).getBitmap();
        myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird2, null);
        bitmap2 = ((BitmapDrawable) myDrawable).getBitmap();
techno
  • 47
  • 1
  • 7
  • 1
    https://stackoverflow.com/questions/3476430/how-to-get-a-resource-id-with-a-known-resource-name So call with `"bird"+ #` in a loop to get the id. – greenapps Apr 06 '18 at 11:08
  • it accepts only actual value not variable – techno Apr 06 '18 at 11:21
  • What do you mean? Please show the code you tried in an extra code block in your post. If the poster of an answer there gets `273` points then it will be good. – greenapps Apr 06 '18 at 11:22
  • The link you provided is depreciated code. – techno Apr 06 '18 at 11:25
  • 1
    My god... if it works use it. In ten years you can think of something new. And i do not believe it's deprecated what i have in mind. You should be much more specific whats deprecated. – greenapps Apr 06 '18 at 11:25

2 Answers2

1

I got it working like this. I'm sure someone can do it much nicer... but this works.

Caveats:

1) Remember to replace "com.example" with your real package name.

2) Replace "numberOfBirdsBitmaps = 3" with however many you have.

3) The "derp" ImageViews are just for my testing purposes.

public class MainActivity extends AppCompatActivity {

int numberOfBirdsBitmaps = 3;

ArrayList<Bitmap> birdBitmapArray = new ArrayList<>();

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

    ImageView derp1 = (ImageView) findViewById(R.id.imageView);
    ImageView derp2 = (ImageView) findViewById(R.id.imageView2);
    ImageView derp3 = (ImageView) findViewById(R.id.imageView3);

    for (int i = 0; i<numberOfBirdsBitmaps; i++) {

        String temporaryString = "bird" + ((Integer) (i+1)).toString();

        int temporaryIdentifier = getResources().getIdentifier(temporaryString, "drawable","com.example");

        Drawable temporaryDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), temporaryIdentifier, null);

        Bitmap temporaryBitmap = ((BitmapDrawable) temporaryDrawable).getBitmap();

        birdBitmapArray.add(temporaryBitmap);

    }

    derp1.setImageBitmap(birdBitmapArray.get(0));

    derp2.setImageBitmap(birdBitmapArray.get(1));

    derp3.setImageBitmap(birdBitmapArray.get(2));


}
}

Love, Boober.

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • `Remember to replace "com.example" with your real package name.` No. use `getPackageName()` as you can see in the other answer. – greenapps Apr 06 '18 at 12:37
0

You can try and get the drawable id using a loop:

for(int=1; i<=9 ; i++)
{
  getResources().getIdentifier("bird"+i,"drawable",getPackageName());
}
Estevex
  • 791
  • 8
  • 17