Android - Open resource from @drawable String
If you really need to work with a string, try something like this:
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
Else I would recommend you to work with R.* references like this:
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Open drawable resource from string
use getResources().getIdentifier
for getting drawable id using string name as :
int drwableid = ctx.getResources().getIdentifier("image1",
"drawable", ctx.getPackageName());
InputStream is = ctx.getResources().openRawResource(drwableid);
Find drawable by string
Yes, you can look it up by name using Resources.getIdentifier()
.
Context context = imageView.getContext();
int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName());
imageView.setImageResource(id);
It's not efficient, but it works to look up occasional resources.
Open a resource file just with specific String variable
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
Else I would recommend you to work with R.* references like this:
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Reference -> Android - Open resource from @drawable String
how to access the drawable resources by name in android
Note that your approach is almost always the wrong way to do things (better to pass the context into the object itself that is using the drawable than keeping a static Context
somewhere).
Given that, if you want to do dynamic drawable loading, you can use getIdentifier:
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(name, "drawable",
context.getPackageName());
return resources.getDrawable(resourceId);
how get Drawable from String path?
The method getDrawable()
is deprecated and the alternative is
ContextCompat.getDrawable(context, R.drawable.***);
But if you have a String
name of the Image. Then here is what you have to do:
getResources().getIdentifier("image_name_in_drawable_folder", "drawable", getActivity().getPackageName());
Do not put the full path from the Uri. Just put the identifier name only the image name only.
Add drawable in resources string.xml
You can do so by providing text using Spannable string just like that
//your drawable
ImageSpan is = new ImageSpan(context, R.drawable.arrow);
//your spannable text "Lorem.... amet"
SpannableString spannableText= new SpannableString("Lorem ipsum dolor sit amet");
//apply image to spannable text
//0 is the flag
//start and end are the index length where you wantg to put the image
spannableText.setSpan(is, startIndex, End index, 0);
//then, you can apply this tspannable text to textview
yourTextView.setText(spannableText);
How to replace R.drawable.someString
this is not a right syntax, and you must getting compiler time error, before altering image resource you must know that, all resources provided an id, these ids are stored into R.java file. Ids are stored into integer format, and in your application you can fetch all these resources by these id, not by name, so you need to fetch id of resource at first, by:
String mDrawableName = "myimg";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
and then use this resID.
Android, getting resource ID from string?
@EboMike: I didn't know that Resources.getIdentifier()
existed.
In my projects I used the following code to do that:
public static int getResId(String resName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
It would be used like this for getting the value of R.drawable.icon
resource integer value
int resID = getResId("icon", R.drawable.class); // or other resource class
I just found a blog post saying that Resources.getIdentifier()
is slower than using reflection like I did. Check it out.
How to access res/drawable/folder
No, Android does not allow subfolders under /res/drawable
: Can the Android drawable directory contain subdirectories?
You can however, add all image files under /drawable
and then access them programmatically via:
int drawableID = context.getResources().getIdentifier("drawableName", "drawable", getPackageName());
iv.setImageResource(drawableID);
Where drawableName
is a name of the drawable file, i.e. myimage
if image file is myimage.jpg
.
The code above will get image resource with id R.drawable.drawableName
.
Related Topics
Arrayadapter in Android to Create Simple Listview
Building Just One Tool from Android Source (Aosp)
Why Does Contentresolver.Requestsync Not Trigger a Sync
How to Prevent a Service from Being Killed by Os
Prevent Status Bar for Appearing Android (Modified)
Google Speech Recognition Timeout
How to Create a Custom Home-Screen Replacement Application for Android
How to Hide Status Bar in Android
How to Add a Badge to an Application Icon in Android
Android Plugin Install Problems in Eclipse
How to Create a Table with Borders in Android
Getting Bitmap from Vector Drawable
Android: Binding Data from a Database to a Checkbox in a Listview
Didn't Find Class "Com.Google.Firebase.Provider.Firebaseinitprovider"
Getview Returning Null When Fragment Has Been Created from an Activity