1

I am trying to display an image when i have variable from array (arr[r1]) which matches the name of the picture. I tried using this but it didnt work:

ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/"+arr[r1], "drawable", getPackageName())));

Any idea what i am doing wrong?

3
  • define "didn't work". format. Commented Feb 21, 2013 at 12:18
  • imageView.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/"+arr[r1], "drawable", getPackageName()))); Commented Feb 21, 2013 at 12:25
  • what is the value of arr[r1]? Commented Feb 21, 2013 at 12:29

3 Answers 3

1
String filePath = getActivity().getFileStreamPath("myimage.png").getAbsolutePath();

img.setImageDrawable(Drawable.createFromPath(filePath));
Sign up to request clarification or add additional context in comments.

Comments

0

Replace this

imageView.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/"+arr[r1], "drawable", getPackageName())));

with

imageView.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(arr[r1], "drawable", getPackageName())));

Hope it works for you.

Comments

0

this work fine for me

imageView.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/ic_launcher", "drawable", getPackageName())));

for your case use this

imageView.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("@drawable/"+arr[r1], "drawable", getPackageName())));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.