i'd like to get filename of shuffled ImageViews
private List<Drawable> images_;
images_ = new ArrayList<Drawable>();
images_.clear();
images_.add(getResources().getDrawable(R.drawable.img1));
images_.add(getResources().getDrawable(R.drawable.img2));
images_.add(getResources().getDrawable(R.drawable.img3));
images_.add(getResources().getDrawable(R.drawable.img4));
Collections.shuffle(images_);
ImageView img_1 = (ImageView)findViewById(R.id.img_1);
img_1.setBackgroundDrawable(images_.get(0));
How to know what image name was setted in this ImageView (img_1) ?
Use setImageResource(resID) instead of setBackgroundDrawable. Now you can get the id of the image as the same resID using the setId(resID) Method. To get the image name use the following method.
getResources().getResourceName(urImage.getId());
ArrayList<Integer> list = new ArrayList<Integer>() {{
add(R.drawable.img1);
add(R.drawable.img2);
add(R.drawable.img3);
}}
Collections.shuffle(list);
ImageView img_1 = (ImageView)findViewById(R.id.urimageId);
img_1.setImageResource(list.get(0));
img_1.setTag(list.get(0));
Now to get the image name do the following.
getResources().getResourceName((Integer)img_1.getTag());
According to the Android Developers website the ImageView is inherited from android.view.View. So you can use getResources() to get a Resources object and then use getAssets() to get the AssetManager.
I'm at work so I can't provide example right now. Sorry. I hope this helps though.
Happy coding! :)
Try this to get file name from an ImageView:
ImageView iv = new ImageView(MainActivity.this);
String path = f.getAbsolutePath();
iv.setImageBitmap(BitmapFactory.decodeFile(path));
iv.setTag(path);
Retrieving the path:
String path = (String) iv.getTag();
Related
I have 170 images in the drawable folder in my android app. I have one activity displaying all of them. What is want to do is to pass the clicked imageview to another activity (Zoom_activity) where the user can zoom it and play around with it. How do I achieve it?
All the images are 500x500px. So I can't think of decoding them into Bitmaps and passing Btmaps via Intent. Please suggest a better and simple way to do it! I have already had a look at the other answers here on SO but none of them solved my problem.
Here is my code:
Activity_1.java
Intent startzoomactivity = new Intent(Activity_one.this, Zoom_Image.class);
String img_name = name.getText().toString().toLowerCase(); //name is a textview which is in refrence to the imageview.
startzoomactivity.putExtra("getimage", img_name);
startActivity(startzoomactivity);
Zoom_Activity.java
Intent startzoomactivity = getIntent();
String img_res = getIntent().getStringExtra("getimage");
String img_fin = "R.drawable."+img_res;
img.setImageResource(Integer.parseInt(img_fin));
Error: App force closes
Please help me solve this problem!
Thanks!
Integer.parseInt() only works for strings like "1" or "123" that really contain just the string representation of an Integer.
What you need is find a drawable resource by its name.
This can be done using reflection:
String name = "image_0";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier():
String name = "image_0";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
What you are trying is wrong. You can not convert "R.drawable.name" with Integer.parseInt. Integer.parseInt is expecting something like "100". You should use
getIdentifier(img_fin, "drawable", getPackageName());
to retrieve the resources id you are looking for
Use getResources().getIdentifier to load image from Drawable in ImageView as:
int img_id = getResources().getIdentifier(img_res, "drawable", getPackageName());
img.setImageResource(img_id);
I wrote the following code:
ImageButton b = (ImageButton) v;
b.setBackgroundResource(R.drawable.tom);
tom.png is in res/drawable. The code above it's working, but I saved the image name in the database (for example "tom"). And I tried the code behind:
InfoDataSource datasourceRuta = new InfoDataSource(this);
datasourceRuta.open();
String strInfo = "";
List<Info> objInfo = datasourceRuta.GetInfo()
for (Info info : objInfo) {
strInfo = info.getImg0();
ImageButton b = (ImageButton) v;
b.setBackgroundResource(R.drawable.strInfo);
}
strInfo = "tom", and I get an error: strInfo cannot be resolved or is not a field.
Do you have a solution for me?
Should I save the image in database?
When you copy "tom" image to drawable folder, Android generates a field in gen/R.java a tom field for that. In this case. There is no strInfo in R.java, so you can't access it.
In this case, you should read from image file, create a Bitmap and setBackground for your button. You can take this for reference: How to read a file into a Java Bitmap?
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());
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.
I am trying to make images dynamic in my android application. With the help of other posts i wrote this code but i am unable to set image resource. Here is my code.
// get image whose source i want to change..
ImageView IV = (ImageView) findViewById(R.id.imageView1);
// x for 1,2,3.. hangman1.png,hangman2.png and so on image are located
// under res/drawable-mdpi
int j = getResources().getIdentifier("hangman"+x, "imageView", getPackageName());
// here i get errork, The method setImageResource(int) in the type ImageView
// is not applicable for the arguments (Drawable)
IV.setImageResource( getResources().getDrawable(j) );
int j = getResources().getIdentifier("hangman"+x, "drawable", getPackageName());
try this, if you are trying to get a drawable. and also as kcoppock said , just use j.
Because you're trying to pass in a Drawable. getDrawable(), as its name states, returns a Drawable object. setImageResource takes a resource ID, which is what getIdentifier will return. You should just pass j to setImageResource().
Edit: Heh, and you also should use Yashwanth's answer, as he said. :)
(TEAMWORK!)
combining both above solutions did the trick for me..here is code..
dnt know which answer i should accept..
ImageView IV = (ImageView) findViewById(R.id.imageView1);
int j = getResources().getIdentifier("hangman"+x, "drawable", getPackageName());
IV.setImageResource( j );
from your xml file , use app:src instead of android:background, and from the activity file, use :
setImageDrawable(getRessource().getDrawable(R.drawable.yourRessource));