Android - getIdentifier always returns 0 (library +application)

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

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.

回答1:

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



回答2:

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



回答3:

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


回答4:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!