Duncan
Duncan

Reputation: 31

How do you get the photo name of a picture in drawable?

How do you get the photo name of a picture in drawable? For instance if the photo was test.jpg then I want to get the name "test" from my image viewer.

ImageView picture = new ImageView(this);
picture.setImageResource(R.id.test);
String hello = picture.getResource.name?

Any help would be appreciate thanks!

Upvotes: 0

Views: 75

Answers (2)

harmanjd
harmanjd

Reputation: 1954

ImageView doesn't keep track of that, but you could extend ImageView and do it yourself. (Using the code from chessdork). Override the setImageResource method.

public class MyImageView extends ImageView {
    private String imageName;

    @Override
    public void setImageResource(int resourceId) {
       imageName = getResources().getResourceEntryName(resourceId);
       super.setImageResource(resourceId);
    }

    public String getImageName() {
       return imageName;
    }
}

Upvotes: 1

chessdork
chessdork

Reputation: 2019

I think getResources().getResourceEntryName(R.id.test) should do the trick.

http://developer.android.com/reference/android/content/res/Resources.html#getResourceEntryName(int)

Upvotes: 0

Related Questions