How to convert a string into a path - android

How to convert a string like "R.drawable.online" into a path, which can represent an image?
What should I do for this?

Take a look at this answer:
Android and getting a view with id cast as a string
By the way... that kind of resources cannot be access using a path... you must use the integer representation of it.

if you have an image inside your folder /drawable/online.png
you will load your image for example into a ImageView...
ImageView.setImageDrawable(getResources().getDrawable(R.drawable.online));

Related

Use dynamic filename in Android R.drawable.<filename>

I'm trying to dynamically construct the filename of an image in Android to use in the R.drawable. clause.
Here's my code:
imgHouse = (ImageView)findViewById(R.id.imageView);
strHouseNameImageFile = strHouseName + ".png";
imgHouse.setBackgroundResource(R.drawable.<filename>);
And in the last line above, I really want it to be a combination of the house name and .png but I'm not sure how to put a string together that will be used by R.drawable.
Could I do something like this for the last line, which now gives an error in Android Studio:
imgHouse.setBackgroundResource(strHouseNameImageFiles);
Any help would be appreciated.
Thanks
int iconResId = getResources().getIdentifier(strHouseName, "drawable",getPackageName());
imgHouse.setImageResource(iconResId);
The way you are trying to set the background resource may not work since the R class is generated at compile time. The R.drawable.*** is really an integer which is generated at compile time. The closest match to your problem may be this post : Dynamic loading of images R.Drawable using variable

How to add R.drawable resources to an array for android?

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

Saving resources path in SQLite

I need to save the images in my resource folder in an SQLite database. This database is pre-loaded(copied to data/ path) so the there is no populating at runtime. I've tried to save it like this in text fields in SQLite: "R.drawable.picture1".
There are about 300 small size pictures(jpg) in my drawable folder. When I run the a query ofcourse the cursor returns the particulair string. Now I need to set this as ImageResource on an ImageView, but whatever I try it doesn't seem to work.
I've tried parsing it to an int, what doesn't seem to work. Whatever I try logcat keeps telling me R.id.picture1 isn't a valid integer. I've seen a possible solution some time ago, but I've searched for it all day but can't seem to find it.
What I dont want: Convert the images to byte arrays so I can save it as a blob. I just need a way to store and retrieve an drawable reference that I can use to set the drawable on the ImageView.
I'm not that experienced so I hope someone can help me out.
If you have the name of the resource you can lookup the resource identifier and use that for your ImageResource.
int resId = context.getResources().getIdentifier("picture1","drawable",context.getPackageName());
image.setImageResource(resId);;

Is it possible to add images programmatically?

I have a web service where I read various information from. One of these pieces of information is the name of an image. I have all the images stored locally in the drawables folder. What I have done right now is I read the string of the image I need from the web service. I want to use this string name and insert the image with the same string name from my drawable folder. Is this possible to do?
Is it possible to do the following somehow?
//Setup the image
ImageView spotIvIcon = (ImageView)findViewById(R.id.spot_selected_image);
String temp = "R.drawable."+spotImage;
spotIvIcon.setImageResource(R.drawable.beer);
What I do with the temp string. Is it possible to convert this string into the int I need? I have the beer in there just to test something out.
Thanks
So I found out how to do this. If anyone wanted to know how it's done use the following code:
int path = getResources().getIdentifier(spotImage, "drawable", "com.androidpeople.tab");
spotIvIcon.setImageResource(path);
Where:
spotImage is the name of your string
drawable is the type you want
com.androidpeople.tab is your package name
Works like a charm now.
you could just use the assets directory to store these images, and then load them using something like BitmapFactory.decodeStream(assetManager.open(imAnAssetName));
the asset directory it's here to be used for things you don't want to be managed (or that can't be managed) by the resource manager.
http://developer.android.com/reference/android/content/res/AssetManager.html .

Android Bitmap syntax

I want to load bitmaps into ImageViews in Android, but I don't want to use the R.drawable syntax because I have a lot of images with a standard naming convention so it's much easier to get the image with some logic. For example, all my images are named:
img1.png
img2.png
img3.png
So if a user selects a value, let's say x, I show "img" + x + ".png" in the ImageView. Looks like Bitmap.decodeFile is what I need, but I need to know the syntax of how to get to my drawables folder since that's where the images are. Or if there's perhaps a better way.
I realize I could do this with a switch statement instead of concatenating the image name but that would be a lot of lines since I have so many images.
One alternative is to use reflection to load images from R. You can looking static variables by string name. Keep in mind that reflection can be slow though. If you are going to be loading the same image multiple times, you might want to keep a cache of name -> R id.
Edit:
You can also access them by URI. See referring to android resources using uris.
The R.drawable value is a number by itself. You just need to use the first image id and add a fixed value to find the right image. For example:
R.drawable.image1 = 1234;
R.drawable.image2 = 1235;
etc.
So to get image 2 I would to R.drawable.image1 + 1.
Every time the R.java file is regenerated the numbers may change but the sequence will be the same. If you don't want to depend in this then you will have to look at the "assets" folder.
Looks like I've found a solution. The real problem is that I generate the resource names at runtime, but I can get the resource id like this:
getResources().getIdentifier("string1" + string2, "id", "com.company.package")
I can then pass that value to setImageResource or any other method that accepts a resource id.

Categories

Resources