I am trying to get images from my asset folder and setting them to my variable called insectImage. Here is what I've done:
InsectRecyclerAdapter.InsectHolder insectHolder = holder;
Insect insectItem = (Insect) recyclerViewItems.get(position);
String imageName = insectItem.getImageName();
Log.d("imageName",imageName);
int imageResID = mContext.getResources().getIdentifier(imageName, "drawable", mContext.getPackageName());
Log.d("imageID", ""+imageResID);
insectHolder.insectImage.setImageResource(imageResID);
imageResID
returns 0 in the logcat, but the imageName
variable is set to one of my images that exist in my assets folder. Logcat is saying that setImageResource(imageResID) is returning null.
How can I set the imageResID
to the imageName
that's in my asset folder?
If imageResId
is zero, that means that getIdentifier
cannot find a resource with the name information that you gave.
Reference:
That's the root cause of the problem. What happens after that is just "consequences" of the earlier error.
Solution: find out why the resource identifier lookup fails.
name
wrong? defType
? defPackage
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.