You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目中setImageResource动态加载图片失效问题求助

How to Dynamically Load Drawable Images Based on Intent Extra in Android

Hey there! I get exactly what you're trying to pull off—you want to send a drawable image name from one Activity to another and display it dynamically, instead of relying on hardcoded R.drawable.someimage references. The core issue here is that setImageResource() expects an integer resource ID, not a string, so we need to convert that string name into the corresponding integer ID first. Let's walk through this step by step:

Step 1: Pass the Image Name from the Source Activity

First, when launching the target Activity via Intent, pass the raw image name (without file extensions) as an extra. For example, if your image is cat_profile.png in the drawable folder, you'll pass "cat_profile":

// In your source Activity
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("selected_icon", "cat_profile"); // No .png/.jpg suffix!
startActivity(intent);

Step 2: Convert the String Name to a Drawable Resource ID in the Target Activity

In the target Activity, retrieve the string extra, then use getResources().getIdentifier() to fetch the integer ID linked to that drawable name. This method looks up resources by their name, type, and your app's package name:

// In your Target Activity's onCreate() method
ImageView imageView = findViewById(R.id.your_image_view);
String iconName = getIntent().getStringExtra("selected_icon");

if (iconName != null) {
    // Get the corresponding drawable resource ID
    int drawableId = getResources().getIdentifier(
        iconName,
        "drawable",
        getPackageName()
    );

    // Validate the ID to avoid crashes if the image doesn't exist
    if (drawableId != 0) {
        imageView.setImageResource(drawableId);
    } else {
        // Fallback to a placeholder if the requested image isn't found
        imageView.setImageResource(R.drawable.default_placeholder);
    }
}

Key Tips to Avoid Headaches

  • Case Sensitivity: Resource names are case-sensitive! Double-check that the string you pass matches the drawable file name exactly (e.g., CatProfile vs cat_profile are treated as different resources).
  • No File Extensions: Never include .png, .jpg, or any extension in the string—Android's resource system ignores these when generating the R class.
  • Package Safety: Using getPackageName() ensures you're looking up resources in your own app's package, which is way more reliable than hardcoding the package string.
  • Fallback Plan: Always check if drawableId isn't 0 (since getIdentifier() returns 0 if the resource can't be found) to prevent Resources$NotFoundException crashes.

Alternative: Pass the Resource ID Directly (If Possible)

If you have access to the R.drawable IDs in the source Activity, you can skip the string conversion entirely and pass the integer ID directly. This is more efficient and avoids lookup overhead:

// Source Activity
intent.putExtra("icon_id", R.drawable.cat_profile);

// Target Activity
int iconId = getIntent().getIntExtra("icon_id", R.drawable.default_placeholder);
imageView.setImageResource(iconId);

This works best when you know all possible image options in advance in the source Activity.

内容的提问来源于stack exchange,提问作者SANJOY DASGUPTA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:32:22