Android – How to get image resource id from image name

How to get image resource id from image name is one of the challenges we face why developing for android.

Imagine that you have some images stored in the drawable folder and also in local SQLite database. The images are stored with their names in the database. If you want to fetch the image names from the database and in other to use them in your ImageView you need to get the image resource id from the names of the images.

The question in your mind now is if there is any way to find image resource id from image name.

This is how we can get image resource id from image name.

1. Create a new android app

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidImageNameToId

Package: com.inducesmile.androidnametoid

Select Empty Activity

Name your activity: MainActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.

2. Add the code below to activity_main.xml layout file

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.inducesmile.androidimageresourceid.MainActivity">
    <ImageView
        android:id="@+id/image_name_to_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:contentDescription="@string/app_name"/>
</RelativeLayout>

In this tutorial, I am using image named profiles.png which is inside the drawable folder

4. Update your MainActivity.java file

You can see that we are using the name of the image and we also called a method that returns its id. With the image id, pass it as a parameter to ImageView setImageResource(int id) method.

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageNameToId = (ImageView)findViewById(R.id.image_name_to_id);
        int imageId = getResourseId(this, "profiles", "drawable", getPackageName());
        imageNameToId.setImageResource(imageId);
    }
    public static int getResourseId(Context context, String pVariableName, String pResourcename, String pPackageName) throws RuntimeException {
        try {
            return context.getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
        } catch (Exception e) {
            throw new RuntimeException("Error getting Resource ID.", e);
        }
    }
}

That is simply a way to get image resource id from image name. If you have used another method to achieve it, I will appreciate it if you can share your solution with us.

OTHER INTERESTING POSTS:

Add a Comment