Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have Android project (com.appocaliptic.quizknife.app) which uses Android library (com.appocaliptic.quizknife.core).

What I am trying to do, is to get resource id of the picture which is the library. Path to the image is: res/drawable-xhdpi/fr_200_133.png

However, all tries with getIdentifier result 0. Where is the problem?

resId = getResources().getIdentifier("fr_200_133", "drawable", "com.appocaliptic.quizknife.core");
resId = getResources().getIdentifier("com.appocaliptic.quizknife.core:drawable/"+"fr_200_133", null, null);
resId = getResources().getIdentifier("drawable/fr_200_133", null, "com.appocaliptic.quizknife.core");

Edited:

Ach, and in R.java there is drawable and corensponding attribute.

@Squonk - That shouldn't matter when it comes to retrieving the id itself; only in retrieving the drawable associated with the id. Besides, for drawable resources, the system will find the best match, so it will use the xhdpi resource (after scaling) even on an ldpi device. – Ted Hopp Sep 12, 2012 at 20:24

You should not be using the library package name. Try this instead:

resId = getResources().getIdentifier("fr_200_133", "drawable", getPackageName());

(or getContext().getPackageName() if this is executing in a view).

The key is that you need to use the app's package name (as listed in the manifest) rather than the library's package name (which actually disappears when creating the app).

wow, it works O_o. Thanks a lot - btw - any source of this information ? All resources which I found they were suggesting use String package name. Thx. – bluszcz Sep 12, 2012 at 22:19 @bluszcz - I think I picked up that idiom from an example somewhere. The key is that you need to use the app's package name (as listed in the manifest) rather than the library's package name (which actually disappears when creating the app). – Ted Hopp Sep 13, 2012 at 0:06 I believe you should post your comment along with the reply - this is very crucial information ;) – bluszcz Sep 13, 2012 at 8:24

I was getting the same error and the only thing that worked was going about it in a different way:

resourceId = R.drawable.class.getField("fr_200_133").getInt(null);

I had a similar issue. I could resolve it similar to what Hussam Otri mentions. For example:

//This doesn't work
context.getResources().getIdentifier("audio_1.mp3", "raw", this.getPackageName()); 
//This works (strip off the file extension)
context.getResources().getIdentifier("audio_1", "raw", this.getPackageName());
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.