Is it possible to add images programmatically? - android

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 .

Related

how to iterate over all drawable of another APK's resources without knowing their resources name

I couldn't find how to list all drawable inside another app from my app.
I managed to get drawables if I know the name, but couldn't find how to do it with a simple loop from 1 to N.
Any of you could offer a code template to do this?
The following code already works:
mAndromedaAddonContext = createPackageContext( "com.demo.andromeda",
Context.CONTEXT_IGNORE_SECURITY );
mPlanetsResID[0] = mAndromedaAddonResources.getIdentifier( "planet20111007081421628", "drawable", "com.demo.andromeda" );
But I want to iterate without using the resource name.
Something along those lines:
for(int i=0; i
I do have the same sharedUserId and process in the manifest file, so I have the privilege to access the resources from the other app.
Thanks in advance.
First use mAndromedaAddonResources.getAssets() to get an instance of AssetManger for the app you are targeting.
see here:
https://developer.android.com/reference/android/content/res/Resources.html#getAssets()
Then you can use the
public final String[] list (String path)
method in AssetManager class to get a list of all assets at a given path( In your case, it would be the path of the res/drawable folder for the app you are targeting). This will give you a list of strings where each string is an asset name. Then you can simply use the same code you are using to obtain their identifiers.
For more info on AssetManager:
https://developer.android.com/reference/android/content/res/AssetManager.html

reading image path from a database

I'm stuck in something that I'm sure rather easy
I have a field in the database that I stored the image name in it, ex 'images/ex1.png'
how can I read this into an imageView?? I tried some code in there but can't get it to work
a good example is highly
You cannot set the image file directly as the resource for your ImageView (that only takes the resource ID).
Instead you will have to use a BitmapFactory to decode it and then use setImageBitmap
EDIT :
Your problem is that there is no way for you to map the name of the image file with the resource ID in the code. The easier way is probably for you to put together a map table.
First put your image files in the drawable folder (res/drawable/ex1.png...) and then, do something like this in your code :
Map map = new HashMap();
// Add key/value pairs to the map
map.put("images/ex1.png", new Integer(R.drawable.ex1));
map.put("images/ex2.png", new Integer(R.drawable.ex2));
// do that for all the images....
String image_name = // query to your database
ImageView the_image_to_change = // findViewById(...)
if (map.containsKey(image_name)) {
the_image_to_change.setImageResource((Integer)(map.get(image_name)).intValue()); // not sure so much is necessary...
}
If you're storing this in your /res/ folder, you need to use a ContentProvider in order to get access to this resource. You shouldn't be accessing resources directly unless you're downloading them externally from the app.
Hope this helps.
myImageView.setImageBitmap(BitmapFactory.decodeFile(myFilePathString));
Your issue is that your file path does not include the root of the device's file system. You have to add this to the beginning of your file path string. This depends on how you save the images. So...
To acquire the appropriate file path to a file on the sd card:
File sdCard = Environment.getExternalStorageDirectory();
String filePath = sdCard.getAbsolutePath() + imageFilePath;
If you're saving your images to internal memory, your app is automatically given a folder to save files to and they will be located there. To find the root to that path use getFilesDir(). For more information on data storage check out http://developer.android.com/guide/topics/data/data-storage.html.
Use the res/drawable folder. Here's how to load an image from drawable to a Bitmap.
Bitmap myImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_image);
More on resources: http://developer.android.com/guide/topics/resources/providing-resources.html

Sorting Images on Android

I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.

How to convert a string into a path

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));

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