Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - getIdentifier always returns 0 (library +application)

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.

like image 584
bluszcz Avatar asked Sep 12 '12 19:09

bluszcz


2 Answers

I faced the same problem: "getIdentifier result 0" and i solved it by removing image extension (*.jpg, *.jpeg,... etc) to be match the name as it in R.java file

like image 66
Hussam Otri Avatar answered Sep 20 '22 12:09

Hussam Otri


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).

like image 30
Ted Hopp Avatar answered Sep 20 '22 12:09

Ted Hopp