bluszcz
bluszcz

Reputation: 4128

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.

Upvotes: 18

Views: 19764

Answers (6)

Abdullah Programmer
Abdullah Programmer

Reputation: 75

In my case, Language was Turkish and it replaced letter 'i' with 'ı' for all resource files. I replaced 'ı' with 'i' in the string resource name. Now my app is working well. Code :

public static int getResId(String imageName ,Activity activity) {
        int resourceId = activity.getResources().getIdentifier(imageName, "drawable", activity.getPackageName());
        if (resourceId == 0) {
            imageName = imageName.replace("ı", "i");
            resourceId = activity.getResources().getIdentifier(imageName, "drawable", activity.getPackageName());
        }
        return resourceId;
    }

Upvotes: 0

aboo
aboo

Reputation: 99

The name of image is underscore (underline) or "_" and I result okay because compiler rename name of photo as sample: source:"img0001" after compile "imag_0001".

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

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

Upvotes: 33

Prasanna Ramaswamy
Prasanna Ramaswamy

Reputation: 231

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());

Upvotes: 9

Joshua Pinter
Joshua Pinter

Reputation: 47481

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

Upvotes: 13

Hussam Otri
Hussam Otri

Reputation: 501

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

Upvotes: 40

Related Questions