Dinesh Hebbar
Dinesh Hebbar

Reputation: 68

Resources $NotFoundException: Resource ID #0x0

I am storing the image name with extension in sqlite example("food.xml").

when i fetch the image name and try to set it to ImageView i am getting this error.

My Adapter Class

String uri = "@drawable/" + expenseCategory.getCategory_image();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
cat_img.setImageDrawable(res);

XML File

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp">

<LinearLayout
    android:id="@+id/image_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circleyellow"
        android:padding="5dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/food" />
</LinearLayout>


<TextView
    android:id="@+id/item_cat_text"
    android:layout_width="50dp"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image_layout"
    android:text="Food"
    android:textColor="#000"
    android:textSize="12sp" />
</RelativeLayout>

Upvotes: 0

Views: 673

Answers (3)

Emre Akt&#252;rk
Emre Akt&#252;rk

Reputation: 3346

Remove the extension from URI variable. You should uri should look like @drawable/food

context.getResources().getIdentifier("@drawable/food", null, context.getPackageName());

is same with

context.getResources().getIdentifier("food", "drawable", context.getPackageName());

Also make sure to check your resId is not 0

Upvotes: 1

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

You are doing it all wrong..

try this way

int resourceId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
if (resourceId != 0)
    cat_img.setImageDrawable(context.getResources().getDrawable(resourceId));
else
    cat_img.setImageDrawable(context.getResources().getDrawable(R.drawable.some_icon));

Upvotes: 0

Ezzy
Ezzy

Reputation: 1483

You shouldn't include @drawable/, so assuming that expenseCatetory.getCategory_image(); would return a filename that exists in your drawables, then this should work:

//String uri = "@drawable/" + expenseCategory.getCategory_image();
String fileName = expenseCategory.getCategory_image();
//int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
int imageResource = resources.getIdentifier(fileName, "drawable", context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
cat_img.setImageDrawable(res);

Upvotes: 2

Related Questions