I have stored image icons in res/mipmap folder of my android project. Each of them have their own image id. I want to check if I have image there by image id. I want to do something like,
String input_id = "blue_image";
ImageView image;
if (image.getImageByID(input_id)){ ## if image with blue_image exists
image.setImageResource(...);
}
If I know that blue_image exists then I usually get it like
image.setImageResource(R.mipmap.blue_image);
There seems to be 2 problems in this question. One is about checking image exists by ID and second one is to find something similar to getattr() python function in java.
If you have any other solutions to similar case, those are also welcome.
Try this in your Activity class:
int imageId = getResources().getIdentifier(inputId, "drawable", getPackageName());
if (imageId > 0) {
image.setImageResource(imageId);
}
The point is that getIdentifier() returns 0 if no resouce was found.
Use:
int imageId = getResources().getIdentifier(inputId, "mipmap", getPackageName());
With "mimap", not "drawable" as the other answer suggests
Related
I'm working on an android app using java in AndroidStudio. I have an ImageView pic and I want to set its image as a .png that I have saved in my /drawable folder. I may try
pic.setImageDrawable(R.drawable.image_name);
However, setImageDrawable() requires a Drawable, while R.drawable.image_name returns an Integer (the ID of the image).
How may I resolve this?
You can use setImageResource() for this:
pic.setImageResource(R.drawable.image);
More about it here
Use this for java
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
and then set this drawable to your image view
Basically, I'm trying to add an SVG drawable to an ImageView, but I keep getting strange type errors I can't resolve.
// String var "icon" holds the name of the drawable resource
int drawableId = context.getResources()
.getIdentifier(icon, "drawable", MainActivity.PACKAGE_NAME);
// drawableId shows the error:
// 'getDrawable(android.content.Context, int)' in 'android.support.v4.content.ContextCompat' cannot be applied to '(int)'
// I tried using Context instead of ContextCompat, but I got an error about needing API 21
Drawable channelIcon = ContextCompat.getDrawable(drawableId);
// drawableId here shows no error
imageView.setImageResource(drawableId);
Any idea what I'm doing wrong? Basically this is a workaround for needing to use a variable for R.drawable.something
Thanks.
I'm trying to add an SVG drawable to an ImageView
ImageView does not support SVG.
Any idea what I'm doing wrong?
Beyond the aforementioned issue, replace:
Drawable channelIcon = ContextCompat.getDrawable(drawableId);
with:
Drawable channelIcon = ContextCompat.getDrawable(context, drawableId);
Sorry for the title, had troubles describing what I'm trying to do. I've always had this question for Java as a whole, but right now, its specific for Android. Let's say that I am parsing an API and get this special String, let's say "clear". Inside my drawables, I have a clear.jpg that I would like to set as an ImageView programically . That's not the hard part. What I was wondering is if there was a quick way where I Could call on the .jpg with just the variable name? I know I can easily make different if statements, such as:
if(string == "clear")
{
//setImageView to drawable/clear
}
but is there anyway I can do something such as
//setImageView to drawable/string
where string would be clear? I know obviously it would look for a string inside drawable, but is there anyway I could do what I'm describing? Just a general question I had; it was something I always wondered about.
Let me know! Thanks!
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);
Reference -> Android - Open resource from #drawable String
With what i know, the quickest way to call up that picture is putting the
picture's name in the xml file; For instance if you want to change the background
of an imageview, you can just put in your xml file this code below
android:background="#drawable/clear".
Note the image must have been in the drawable folder. Thanks, i hope this helps.
It looks like Java 8 provides limited support for local variable annotations, but its reflection API can't give you access within method bodies. So you'd need to parse the class file itself. And for that, a library like ASM would do the trick.
if(string.equals("clear")){
int resID = getResources().getIdentifier("clear", "drawable", getPackageName());
imageView.setBackgroundResource(resID);
}
Same as other ones at the point which is using getIdentifier method.
imageView.setImageView(getResources().getIdentifier(string, "drawable",
getPackageName()));
However, by doing this, you can process without if-condition block and just specify a Drawable of the value from string directly (I think it is your intention of this question).
I have 3 images, named img1.png, img2.png, img3.png .
Is it possible to load them once at a time clicking on the same button, getting the number of their name?
first button click -> read image names -> img1 is first
second button click -> read image names -> img2 is second
third button click -> read image names -> img3 is third
EDIT:
I explain better, I'm just trying to figure out what's the best approach:
the example above would allow me to just create a method and avoid creating an array of images.
Second approach is to create an array.
Third approach, create a counter and load an image based on the counter value.
Other approaches?
You can use a variable say
int nowDisplayNumber=0;
String imageName="img"+ ++nowDisplayNumber+".png";//or other approach for construct the image id, or image name
.....//display your image use imageName here.
Please notice that if you do not want the image displayed from the beginning again every time you restart your application, you may need to store the latest image number into a file and read it when application begins
int[] imagename = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
In onClick()
switch(view.getId) {
case R.id.button1 :
button1.setBackgroundResource(imagename[0]);
break;
case R.id.button2 :
button2.setBackgroundResource(imagename[1]);
break;
case R.id.button3 :
button3.setBackgroundResource(imagename[2]);
break;
}
I've been trying to get an icon of some APK file, but with no success.
The thing is, I have the APK file path and using that, I want to get its icon (as a Drawable).
Thanx upfront.
Have a look at PackageItemInfo. With that, you can read out the icon for any app installed on the device!
Or even faster: Use
Context.getPackageManager()
to receive a reference on a PackageManager. On that, you can call:
getActivityIcon(Intent i)
The only thing you have still to do is packing the package path in the intent.
Expanding on Matthias' answer, since my original assumed ic_launcher.png:
int icon = 0;
// if you don't know the icon's name:
icon = (new PackageItemInfo()).icon;
// if you do know the icon's name:
icon = R.drawable.ic_launcher; // or whatever your resource id is
Drawable d = getResources().getDrawable( icon );