0

I cant load an image from @drawable to ImageView using String as name of the image.

Here's my code;

String uri = "@drawable/"+imgname;  // image file
            int resID = getResources().getIdentifier(uri , "drawable", getPackageName());
            Drawable drawable = getResources().getDrawable(resID); //<----line with error
            ImageView imageview= (ImageView)findViewById(R.id.hardImg);
            imageview.setImageDrawable(drawable);

error log:

android.content.res.Resources$NotFoundException: Resource ID #0x0

4
  • Remove "@drawable/". You just need the resource name, which is the filename, without the extension. Commented Mar 11, 2017 at 4:35
  • I just did... but still having the same error. Commented Mar 11, 2017 at 4:41
  • does that image name exist in drawable folder? Commented Mar 11, 2017 at 4:49
  • yes. string names are from ext db and I already checked all case if it is case sensitive. Commented Mar 11, 2017 at 4:57

2 Answers 2

3

You don't need "@drawable/". That is what the second parameter to getIdentifier already does.

getResources().getIdentifier(imgname, "drawable", getPackageName());

Will generate the integer resource ID for <your package name>.R.drawable.<imgname>, or 0 if that resource does not exist.

Returns 0 if no such resource was found. (0 is not a valid resource ID.)

Thus, your error message.

Note : Android resources can only contain certain characters.

Sign up to request clarification or add additional context in comments.

7 Comments

okay. so, I made this String uri = imgname; int resID = getResources().getIdentifier(uri , "drawable", getPackageName()); but still having error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hamira.wordmatchinggame/com.example.hamira.wordmatchinggame.ingamehard}: android.content.res.Resources$NotFoundException: Resource ID #0x0
Yes. Read the quote from the docs I provided. I have no idea what imgname is being set to, but obviously that name doesn't exist in your application's res/drawable folder
I tried to give a direct name like imgname = "ac.png". ac.png is clearly exist in res/drawable but I still have the same error again :-(
I tried something else and it showed up! I dont understand.
|
0

This should work -

String uri = "@drawable/"+imgname;  // image file with extension, make sure image name is correct.
int resID = getResources().getIdentifier(uri , null, getPackageName());
Drawable drawable = getResources().getDrawable(resID); 
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);

5 Comments

you mean something like this one? String uri = "@drawable/"+imgname+".png";
@Jay No. Given the file your_image.png and you want R.drawable.your_image. Android doesn't really care you have a png, so @drawable/your_image is the resource
@cricket_007 I must not. imgname holds different image names on button click.. It change the image base on this string name. names are stored in external db.
@Jay You misunderstand my comment. "@drawable/"+imgname+".png" is not correct. That was all i said.
@cricket_007 my bad.

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.