Android - Store drawable ids in a custom XML file - android

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

Related

Got error when I want to take the ImageView from layout and set the image

Hi have an anroid application and I want take the ImageView from layout and set the image But I got the error: android.content.res.Resources$NotFoundException: Resource ID #0x0
I try log and It will show the uri,and imageResource is also 0
ps: the value of Log for building.getImage() is church however, I have church.png in my drawable folder
I will appreciate if you could help
Here is the code:
/* Extract the Building's object to show */
Buildings building = (Buildings) getItem(position);
ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
String uri = "drawable/" + building.getImage();
int imageResource = context.getResources().getIdentifier(building.getImage(), "drawable", context.getPackageName());
Log.e("URI", uri);
Log.e("context.getPackag", context.getPackageName());
Log.e("imageResource", String.valueOf(imageResource));
Drawable image = ContextCompat.getDrawable(this.context, imageResource);
imageCity.setImageDrawable(image);
the first parameter of getIdentifier is the name of the resource, whose id you are looking for, without extension. In your case this is building.getImage() and not uri, which is the concatenation of "drawable/" and the image's name. The fact the you want to look for it inside drawable, is specified in the second parameter of getIdentifier. Change from
int imageResource = context.getResources().getIdentifier(uri, "drawable", context.getPackageName());
to
int imageResource = context.getResources().getIdentifier(building.getImage(), "drawable", context.getPackageName());

Dynamically handling image resource

I want to change the image resource for bitmap dynamically. But I can't call them with name from the drawable class.
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.getImage(foldername + "/" + imagename));
I need to do something like that but I couldn't find the proper way of doing this.,
EDITED
I think I should be more clear. I store my images under the drawable folder and they are separeted into other folders.
For example;
drawable/imageset1, drawable/imageset2,
and I want to change the Image resource for bitmap depending on user input.
For example:
User selects imageset5 from first spinner, and selects image5.png from another spinner.
I hope this will do what you want
BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(foldername + "/" + imagename , "drawable", getPackageName());
EDITED:
while the above code will work only if you follow android rule which doesn't allow sub directories in drawable folder so the above code will work only when directly accessing images from drawable.
BitmapFactory.decodeResource(getResources(), getResources().getIdentifier( imagename , "drawable", getPackageName());
As these links describe
How to access res/drawable/"folder"
Can the Android drawable directory contain subdirectories?
Make one array of image resources
int []a = int[]
{
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
};
Then access it in loop
for(int i=0;i<a.length;i++)
{
// a[i]
}
Is it a resource stored in the drawables folder? Then try this method from BitmapFactory
public static Bitmap decodeResource (Resources res, int id)
Where
res = getResources()
and id is the id of the drawable like
R.drawable.picture
Check it out here
you can't do like this. You have to add all images in drawable & use it like
myImage.setBackgroundResource(R.drawable.imageName)
or after downloading the image from web you can apply as
myImage.setBitmap(BitmapFactory.decodeStream(in));
to get the bitmap where in is InputStream
Just try following:
Your image having name "imagename" must be in the folder
String IMAGE_FOLDER_NAME = "YOUR_IMAGE_CONTAINING_FOLDER";
Now, use following code:
String imagename = "YOUR_IMAGE_NAME";
String PACKAGE_NAME=getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":"+IMAGE_FOLDER_NAME+"/"+imagename , null, null);
System.out.println("IMG ID :: "+imgId); // check this in log
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME); // check this in log
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);

How to Dynamically change drawable's id in setImageResource() Android

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.

Android, reference things in R.drawable. using variables?

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.

Android - Open resource from #drawable String

I have:
String uri = "#drawable/myresource.png";
How can I load that in ImageView? this.setImageDrawable?
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);
First, don't do that, as that #drawable syntax is meaningless in Java code. Use int resourceId=R.drawable.myresource.
If for some reason you do wind up a resource name and need the integer ID, use getIdentifier() on the Resources object.
you can also add your own variable.. in my case scene.name between i followed #pfleidi answers. c stands for context.
String uri = "drawable/" + scene.name;
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
imageList.setImageResource(imageResource);
Long since overdue, but my favorite is to use the Context:
context.getApplicationContext().getResources().getDrawable(R.drawable.placeholder_image)
where placeholder_image is the id of the resource. In your case, R.drawable.myresource.
Context can be an activity or the application. Pretty much wherever you are has reference to the context, which you can use to get the application's context.
In case anyone else comes across the problem I had, I was loading the image name from JSON and then needed to get the identifier. The second parameter of getIdentifier can be set to "drawable". This is a slight variation on #alia-ramli-ramli answer...
String uri = details.getString("circle").toLowerCase();
int imageResource = context.getResources().getIdentifier(uri, "drawable", context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
viewHolder.unit_circle.setImageDrawable(image);
You can check here
int resID = mContext.getResources().getIdentifier(stringfilename, "drawable", mContext.getPackageName());
iv_imageview.setImageDrawable(ContextCompat.getDrawable(mContext,resID));
you can also retrieve image from mipmap by replacing "drawable" with "mipmap" parameter of getIdentifier() method.
I had the same problem with ... the deprecated etc. etc.
I solved in such a way with minimum API 14 okay.
...
...
ImageView imgPoster = viewCache.getImgPoster(resource);
String uri = "drawable/" + film.getPoster();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = ContextCompat.getDrawable(context, imageResource);
imgPoster.setImageDrawable(image);
return convertView;
...
...

Categories

Resources