SetImageDrawable - android

Can someone please help with this. I am generating a random number and based on this random number I want to pick a png file from my res/drawable-mdpi folder to display. This is the code I'm using:
public void DisplayRandomPicture(int randomNumber)
{
String drawableName = "c"+ randomNumber;
ImageView image= (ImageView)findViewById(R.id.imageView1);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(drawableName, "res/drawable-mdpi", getPackageName())));
}
All the files in the res/drawable-mdpi folder start with "c". There is something wrong with the image.setImageDrawable syntax. When I run my app it crashes.

public void DisplayRandomPicture(int randomNumber)
{
String drawableName = "c"+ randomNumber;
ImageView iw= (ImageView)findViewById(R.id.imageView1);
resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);
}
Try above and make sure the image exists with the exact name.

I resolved this issue by using:
int id = getBaseContext().getResources().getIdentifier(drawableName, "drawable", getPackageName());
image.setImageResource(id);

Related

Set image on a button where buttonname is coming from the database

In case I have to set an image on a button, programmatically, then I use the following code:
Drawable img;
ToggleButton tb_button = (ToggleButton) findViewById(R.id.tglButton);
img = ResourcesCompat.getDrawable(getResources(), R.drawable.p189, null );
tb_button.setCompoundDrawables(img,null,null,null);
Now the situation is that I have read the name of the image from a database into a variable. Thus lets assume that my variable has the following value:
String img_str= "p189";
Now how do I set the same image on the button when the image name is stored inside a variable.
use following method to get image from string.
private int getImageFromString(String name) {
int resId = getResources().getIdentifier(name, "drawable", getPackageName());
return resId;
}
You can create drawable resource id reference dynamically as follows
int resId=getResources().getIdentifier(img_str, "drawable", context.getPackageName())
img = ResourcesCompat.getDrawable(getResources(), resId, null )
and of course don't forget about try catch in case of wrong resource name
You can get drawable as follows by itss name,
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(imgName, "drawable", context.getPackageName());
return resources.getDrawable(resourceId);
So elaborating on what ABK said. You can get the the Drawable ResId and then use that to get the drawable within a function like this:
private Drawable getDrawableFromString(String name) {
int resId = getResources().getIdentifier(name, "drawable", getPackageName());
return getResources().getDrawable(resourceId);
}

Android: How check if a image exists in a IF clause?

I need to know how check if an image exists in R.drawable, It has to be dynamic so I have to use a string that gives me the name of the image.
I've tried with '!=null' or exist but it hasn't worked.... Help please!!!!!!!!!
titulo=bundle.getString("titulo");
textView = (TextView) findViewById( R.id.textView1);
textView.setText(titulo);
foto="f"+bundle.getString("numero")+"a";
System.out.println(foto);
flipper =(ViewFlipper) findViewById(R.id.vfFlipper);
this gives me the name of the image a need...
image = new ImageView(this);
image= new ImageView(this);
image.setImageResource(R.drawable.f00a1);
image.setScaleType(ScaleType.FIT_CENTER);
flipper.addView(image);
Whith this I can use the image but i need to use the variable "foto" so it can be dynamic
Thanks!
Everything in the R class is an integer - you can't create a string to represent a resource id. The closest you can get is to use getResources() then call...
getIdentifier(String name, String defType, String defPackage)
...this will allow you to find the integer which represents your resource based on the resource name.
you could use getResources to get an instance of Resources class. In Resources class, you have getDrawable If the resource is not found, you would get ResourceNotFoundException which also means the image is not found.
so the code will be something like this
Resource r = getResources();
Bool fileFound = true;
Drawable d = null;
try{
d = r.getDrawable(your_image_id);
}
catch(ResourceNotFoundException e){
fileFound = false;
}
if(findFound){
// Your operations
// set drawable to your imageview.
}
Thank you all! i mix the two answers and .. it work!
Resource r = getResources();
Bool fileFound = true;
Drawable d = null;
try{
d = r.getDrawable(getIdentifier(foto1, "drawable", getPackageName());
}
catch(ResourceNotFoundException e){
fileFound = false;
}
if(findFound){
// Your operations
// set drawable to your imageview.
}
getResources().getIdentifier("icon", "drawable", "your.package.namespace");
check for non zero value for above statement.
if(0)
//does not exists
else
//exists
private boolean isDrawableImageExists(String imgName) {
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
return id != 0;
}

dynamically getting all image resource id in an array

there are many images in drawable foder so instead manually creating array of all image resource ids , i want to get all images dynamically of drawable folder in array.
currently i m using this code:
for(int i=1;i<=9;i++)
{
int imageKey = getResources().getIdentifier("img"+i, "drawable", getPackageName());
ImageView image = new ImageView(this);
image.setId(imgId);
image.setImageResource(imageKey);
image.setScaleType(ImageView.ScaleType.FIT_XY);
viewFlipper.addView(image, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imgId++;
}
but in that code i need to manually edit the image name to get the resource id but i want to get all image with any name..
you can Use Reflection to achieve this.
import the Field class
import java.lang.reflect.Field;
and then write this in your code
Field[] ID_Fields = R.drawable.class.getFields();
int[] resArray = new int[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
try {
resArray[i] = ID_Fields[i].getInt(null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
resArray[] now holds references to all the drawables in your application.
Well, If your image names are img1, img2 and so on, then you can create a variable like
String url = "drawable/"+"img"+i;
int imageKey = getResources().getIdentifier(url, "drawable", getPackageName());
you can also replace your getPackageName() method by your package name like "com.android.resource"
Simply,the general function is
public int getIdentifier(String name, String defType, String defPackage)

Select the resource for findViewById(R.id.xxx) in code

This is my first post, sorry for the title and the explication. Sorry for my English.
I define an .xml with all I need. I have 10 ImageView and the id for 10 ImageView are myimage01, myimage02, ... , myimage010.
I need to select an image for an ImageView.
I can do it as follows:
String imageName=getImageName();
id = getResources().getIdentifier(imageName, "drawable", getPackageName());
drawable = res.getDrawable(id);
ImageView cant1= (ImageView)findViewById(R.id.myimage01);
cant1.setImageDrawable(drawable);
ImageView cant2= (ImageView)findViewById(R.id.myimage02);
cant2.setImageDrawable(drawable);
ImageView cant3= (ImageView)findViewById(R.id.myimage03);
cant3.setImageDrawable(drawable);
ImageView cant4= (ImageView)findViewById(R.id.myimage04);
cant4.setImageDrawable(drawable);
ImageView cant5= (ImageView)findViewById(R.id.myimage05);
cant5.setImageDrawable(drawable);
ImageView cant6= (ImageView)findViewById(R.id.myimage06);
cant6.setImageDrawable(drawable);
ImageView cant7= (ImageView)findViewById(R.id.myimage07);
cant7.setImageDrawable(drawable);
ImageView cant8= (ImageView)findViewById(R.id.myimage08);
cant8.setImageDrawable(drawable);
ImageView cant9= (ImageView)findViewById(R.id.myimage09);
cant9.setImageDrawable(drawable);
ImageView cant10= (ImageView)findViewById(R.id.myimage010);
cant10.setImageDrawable(drawable);
But this is too bad, its better to use a loop. But I don't know do this.
I need something like that:
String cad;
for(int i=0;i<10;i++){
cad="myimage0";
String cat= Integer.toString(i);
cad=cad.concat(cat);
ImageView cant1= (ImageView)findViewById(R.id.cad);
cant1.setImageDrawable(drawable);
}
But there is an error in:
ImageView cant1= (ImageView)findViewById(R.id.***cad***);
Thanks for all
You can't directly pass in a String like that to findViewById. You'll need to look up the resource from your String first. Try this:
int idResource = getResources().getIdentifier(cad, "id", getPackageName());
ImageView cant1= (ImageView)findViewById(idResource);
Use this in a loop:
getResources().getIdentifier(resName, "id", getPackageName());
Where resName is exact string for that id (in your example, it's myimage01, myimage02, ...
so we have:
String cad;
for(int i=0;i<10;i++){
cad="myimage0";
String cat= Integer.toString(i);
cad=cad.concat(cat);
int id = getResources().getIdentifier(cad, "id", getPackageName());
ImageView cant1= (ImageView)findViewById(id);
cant1.setImageDrawable(drawable);
}

How is it possible to implement read the image name by clicking on it

Have a code that displays a random image:
Random rand = new Random();
int rndId = rand.nextInt(24) + 1;
imgName = "drw" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imageView.setImageResource(id);
How is it possible to implement read the image name by clicking on it in the program and create a new window with a description of the image that is unique to each.
Yes, you can get the Image Name if you have its resource id by using getResourceEntryName(resource_id),
String image_name = getResources().
getResourceEntryName(R.drawable.ic_launcher);
Log.d("name", image_name);
OUTPUT:
ic_launcher

Categories

Resources