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);
Related
I have few images in my app ( they are all copied into res/drawable and have appropriate R.drawable reference) that I want to assign them to my ImageViews using contents of an string type variable. To illustrated, I have an image file named "c4.png" in my drawable folder and an string variable (card) that contains "c4". I want to see if there is any way I can use a code like this (ImageView1.setImageResource(R.drawable.card)) to assign image c4 to my ImageView1 instead of using ImageView1.setImageResource(R.drawable.c4). Basically, I am wondering if it is possible to replace a variable with the specific image name in the R.drawable.resourceName. I know that R.drawable.resourceName is an integer and I am not trying to change its type. I want to replace the resourceName with a variable. I appreciate any help
Use this to draw a random number in particular range-
int min = 0;
int max = 9;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
Then using this number pick image from image array-
public Integer[] nImages = {
R.drawable.c1, R.drawable.c2,
R.drawable.c3, R.drawable.c4,
R.drawable.c5, R.drawable.c6,
R.drawable.c7, R.drawable.c8,
R.drawable.c9, R.drawable.c10
};
ImageView1.setBackgroundResource(nImages [i1]);
Hope this works.
Try this
Resources res = getResources();
int id = res.getIdentifier(card, "drawable", getPackageName());
img.setImageResource(id);
this may solve your problem.
Or you can put the images in the asset folder. Assets are referenced by their filename, as in
getAssets().open(<filename>);
This returns an InputStream. Afterwhich it is trivial to convert them to a Bitmap resource
BitmapFactory.decodeStream(inputStream);
And then use that bitmap for your ImageView. That should be straightforward with
imageView.setImageBitmap(bitmap);
I want to replace the resourceName with a variable.
Instead of declaring the variable card as a string declare it as an Integer. So that you can use it like the following.
int card=R.drawable.c4;
ImageView1.setImageResource(card);
You probably can not do something like R.drawable.card because the R file is generated according to the resources in the drawable folder.
hope this helps
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
If I have a variable with the number associated with a gridview position (id int 14) how do I make the code below load the correct drawable? (ie hccat14)
mBitmap = getImageFromResource(getContext(), R.drawable.hccat14,w, h);
Thanks,
Shannon
I don't know exactly how you are trying to use it, but it should be something like this.
String resouceName = "hccat" + Integer.toString(14);
int resourceID = getResources().getIdentifier(resourceName,
"drawable", getPackageName());
Here's what I use, just set the stringName on click
imageResource = Classname.this.getResources().getIdentifier(stringName,
null, null);
mBitmap.setImageResource(imageResource);
EDIT I store the image name as packagename:drawable/imagename in my database
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.