Replace Android Studio Image View Image

Asked 2 years ago, Updated 2 years ago, 96 views

Using the setImageResource() function

logoImageViews[i].setImageResource(R.drawable.premierleague);

But what I want is not a tag, but a file name ('xx'.Is it possible to change it by typing 'png')?

android imageview

2022-09-20 20:14

1 Answers

It may be a little complicated to access the file name, but there is a way to use setImageUri().

The format can be "android:resource//package/drawable/file name." If the package is "com.example.package" and the file name you want to recall is "premierlegue.png," write "android:resource//com.example.package/drawable/premierlegue.png."

Then, hand over the Uri object created using the setImageUri() method of the ImageView class.

Uri pathToImage = Uri.parse("android:resource//com.example.package/drawable/premierleague.png");

// Uri made in setImageUri().You can hand over the Uri object created through parse().
logoImageViews[i].setImageUri(pathToImage);

There's another way, but it's a little expensive, so you can experience lag if you use it in places like RecyclerView.

This is a method of using getResources().getIdentifier() in Activity, you can hand over the file name, resource type, and package when you call.

int imageResourceId = getResources().getIdentifier("premierleague.png", "drawable", getPacakageName());
// You can get the package name with getPacakageName() or you can write it yourself.

logoImageViews[i].setImageResource(imageResourceId);


2022-09-20 20:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.