Android项目中setImageResource动态加载图片失效问题求助
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.,
CatProfilevscat_profileare 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 theRclass. - 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
drawableIdisn't 0 (sincegetIdentifier()returns 0 if the resource can't be found) to preventResources$NotFoundExceptioncrashes.
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

