I think i'm getting closer but still getting resource errors with this. I have an image file named rock.png in all 3 drawable folders.
in my layout MAIN.XML:
<ImageView android:id="#+id/rockId" android:src="#drawable/rock"></ImageView>
In my code:
int resID = getResources().getIdentifier("rockId" , "id", "com.testing");
ImageView image = (ImageView) findViewById(resID);
I'm still seeing this in my error catlog:
10-30 17:36:24.485: WARN/ResourceType(74): Resources don't contain package for resource number 0x7f020000
Any thoughts on what I might be doing wrong? Any tips welcome
to find the control:
ImageView image = (ImageView) findViewById(R.id.rockId);
To dynamicly load an image from drawable i use this function
public static int getDrawable(Context context, String name)
{
Assert.assertNotNull(context);
Assert.assertNotNull(name);
return context.getResources().getIdentifier(name,
"drawable", context.getPackageName());
}
this will return the id of your drawable, now all you need to to is set the image in the control:
image.setImageResource(int Id);
Related
I cant load an image from #drawable to ImageView using String as name of the image.
Here's my code;
String uri = "#drawable/"+imgname; // image file
int resID = getResources().getIdentifier(uri , "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(resID); //<----line with error
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);
error log:
android.content.res.Resources$NotFoundException: Resource ID #0x0
You don't need "#drawable/". That is what the second parameter to getIdentifier already does.
getResources().getIdentifier(imgname, "drawable", getPackageName());
Will generate the integer resource ID for <your package name>.R.drawable.<imgname>, or 0 if that resource does not exist.
Returns 0 if no such resource was found. (0 is not a valid resource ID.)
Thus, your error message.
Note : Android resources can only contain certain characters.
This should work -
String uri = "#drawable/"+imgname; // image file with extension, make sure image name is correct.
int resID = getResources().getIdentifier(uri , null, getPackageName());
Drawable drawable = getResources().getDrawable(resID);
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);
In my app I'm using an xml file to store some data, and images are stored in the drawable folder. I would like to "link" these to, meaning that each object in the xml file to refer a certain image in the drawable folder. How could i acomplish this ?
Thanks!
<contacts>
<contact>
<name>Joe</name>
<picture>R.drawable.joe_pic</picture>
</contact>
<contact>
<name>Dean</name>
<picture>R.drawable.dean_pic</picture>
</contact>
</contacts>
you can try with only saving the name like "joe_pic" and can get this way
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);
}
but if you have already added "R.drawable.joe_pic" complete can use String.split to get 3 rd string part
I am working on Application in which I want to access the id of image from R.java which is used by imageView.
for examle
imageView i;
i=(ImageView)findViewbyid(imageview1);
i.setImageResource(R.drawble.human);
.....
now i want to know which id is used by i in running application.
//when you place an image in you drawable it will generate an Id in R.java file
when you want that particular image to display in your ImageView
you need to declare like this
public static Integer[] images = new Integer[]{R.drawable.my_love,R.drawable.first,R.drawable.second,R.drawable.third};
And then set the image resource like this,
ImageView i = new ImageView(this.context);
i.setImageResource(this.images[1]);
//you have declared as
imageView i=(ImageView)findViewbyid(imageview1);
//what is this imageview1?
instead of that you need to give your xml declaration ImageView id as
imageView i=(ImageView)findViewbyid(R.id.imageview);
If you are looking for a method to get the Image Background of a ImageView this will help you.
ImageView imageView=(ImageView)findViewById(R.id.image);
imageView.setBackgroundResource(R.drawable.ic_launcher);
imageView.setDrawingCacheEnabled(true);
Drawable d=imageView.getBackground();
This will give you the id of the image in drawable
String mDrawableName =Integer.toString( R.drawable.human);
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
My Question is how to change image id from drawable folder in setImageResource() in android.
My drawable folder contains icon0.png to icon9.png and i want to change these images in image view dynamically using this
ImageView iV3;
iV3 = (ImageView) findViewById(R.id.imageView3);
iV3.setImageResource(R.drawable.icon + speed_Arr[2]);
speed_Arr[2] contains any value from 0 - 9.
But this didnt change images.
Plz help me out.
regards.
public static int getIdentifier(Context context, String name)
{
return context.getResources().getIdentifier(name.substring(0, name.lastIndexOf(".")), "drawable", context.getPackageName());
}
Above code will return the resource id from name String.
int res = getResources().getIdentifier("< packageName:drawable/imageName >'", null, null);
Use this res in your iV3.
say I want to dynamically load an image file in R.drawable.* based on the value of a string
Is there a way to do this? It seems like I need to statically refer to anything in R.
Have you declared the id for the image in XML file? If you did, you can use the following method:
Let's say you have picture.png in your res/drawable folder.
In your activity, you can set your image resource in the main.xml file
<ImageView android:id="#+id/imageId" android:src="#drawable/picture"></ImageView>
In FirstActivity
//to retrieve image using id set in xml.
String imageString = "imageId"
int resID = getResources().getIdentifier(imageString , "id", "package.name");
ImageView image = (ImageView) findViewById(resID);
imageString is the dynamic name. After which you can get the identifier of the dynamic resource.
Another method, you can do this:
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
You will be able to reference your image resource and set your ImageView to it.
int drawableId = getResources().getIdentifier(drawablename, "drawable", getPackageName());
imageview.setImageResource(drawableId);
Try this. This should work.
Class res = R.string.class;
Field field = res.getField("x" + pos);
headerId = field.getInt(null);
header.setText(headerId);
this works with drawables as well, just edit the string. the header part is not required, it's just an example pulled from something I wrote a while ago.