Hers
Hers

Reputation: 31

How do I access my images in my drawable folder using the name of file?

Good evening the fact is that I can not access my images by the names of the files, if not by a random number (?) Assigned in android, I realized this when by means of a toast I saw that the method:

Uri .parse ("android.resource: //" + R.class.getPackage (). getName () + "/" + portrait_artist) .toString (); 

returned the string: "android.resource: //test.com.myapplication/2131165274" instead of the String android.resource://test.com.myapplication/nameofmyfile.png

and then I do not know how to access my file by name original, I am currently accessing the number that returns, but I need to add other images to the project and I would like to add them by the file name, sorry if my question is ridiculous I am new in this android, I add, I'm using Piccaso visualize the image , Thanks in advance.

Upvotes: 0

Views: 506

Answers (3)

Andronymous
Andronymous

Reputation: 145

well you have the image name and you can use this method to access the image from drawable directory.

public Drawable getImage(String imageName) {
    Resources resources = context.getResources();
    final int resourceId = resources.getIdentifier(imageName, "drawable",
            context.getPackageName());
    return resources.getDrawable(resourceId);
}

Upvotes: 2

Muhammad Hashir Anwaar
Muhammad Hashir Anwaar

Reputation: 614

Try this

int imageRes[] = {R.drawable.image1, R.drawable.image2,...};

then

for(int i =0; i<3;i++){

  //your toast or content here

  imageArray[i] = imageRes[indices[i]];

  //use images where ever you want
}

That may help you... :)

Upvotes: 1

Related Questions