Check two drawable images - android

All
I have created frame animation using Animation drawable. I have 25 images in that animation. Now I have to compare Image which is the current frame in animation with the one that is stored in the res/drawable folder.
How to compare this two drawables?? == and .equals methods wont work with the drawables.
Please give me reply as soon as possible.

you can provide id to the drawable image by using "imageview.setid" and try to compare that given id of images by using "imageview.getid"(use any int for id)

if You want to compare two drawable then use following code
i.e :
drawable1 = context.getResources().getDrawable(R.drawable.ic_launcher);
drawable2 = context.getResources().getDrawable(R.drawable.ic_launcher);
if (drawable1.getConstantState().equals(drawable2.getConstantState()))
{
//write your code.
} else {
//write your code.
}

Related

How to add image background using String path from database in android studio?

Ok, after searching and browsing over the internet, I just didn't get what I was looking for. I have a database with a field for "Path", where my images file names are listed.
**Table1:**
PATH ID
imgRice.jpg 1
imgMango.jpg 2
imgBanana.jpg 3
Now I want my imageView to change picture accordingly from my query:
"Select Path from table1 where ID = 1";
I will declare a String, like String queryResultString to handle the result (ex.imgRice.jpg).
I want a code that looks like imgView.image="drawable/" + queryResultString
Any help is appreciated. Thanks guys!
Thanks for the comments and suggestions guys, highly appreciated.
But I've got a simple yet tricky turnaround for my problem.
First, it is IMPOSSIBLE to rip the Drawable path because it is compressed when published to .apk file.
Second, to make it possible, you need to use id for every file in your drawable.
Third, it is much better to add files(images) to asset folder(\app\src\main\assets).
Lastly, follow these simple codes:
//code to initiate the object ImageView...
ImageView myImage = (ImageView) findViewById(R.id.pic);//pic is the id of ImageView in my xml file...
try
{
// now, get the input stream
InputStream input= getAssets().open(c.getString(c.getColumnIndex("image")));//image is the name of field in my db which holds the path (ex. rice.jpg)...
// initiate image as drawable
Drawable draw = Drawable.createFromStream(input, null);
// set image to ImageView
myImage.setImageDrawable(draw);
}
catch(IOException ex)
{
return;
}
Enjoy Coding! ^_^

Android Live Wall Papers that change random pictures from drawable

Can someone give such a code or at least an idea how to make such wallpapers.
I guess I got to use Vieflipper but I cant use Random with this. Am I right? Thanks.
Your question looks a bit unspecific. In any case, to put it simple, let's assume you have an ImageView that you want to change dynamically in a random manner. The structure should look something like this:
imv = (ImageView)findViewById(R.id.imageView1);
Resources rsrc= getResources();
imid = R.drawable.yourimagename;
Drawable drawable= rsrc.getDrawable(imid);
Bitmap bm = BitmapFactory.decodeResource(rsrc,drawable);
imv.setImageBitmap(bm);
In this example, imid is hardcoded, but you could easily change it dinamically. For instance, create an array list of integers containing your desired images ids and then access them in a loop or whenever a condition is met (for instance every 30 seconds).
Eg:
ArrayList<Integer> listofimagesids = new ArrayList<Integer>;
listofimagesids.add(R.drawable.yourimagename1)
listofimagesids.add(R.drawable.yourimagename2)
...
listofimagesids.add(R.drawable.yourimagenameN)
Random rand = new Random();
if (youcondition){
int n = rand.nextInt(listofimagesids.size());
Drawable drawable= rsrc.getDrawable(n);
Bitmap bm = BitmapFactory.decodeResource(rsrc,drawable);
imv.setImageBitmap(bm);
}
Of course this is just pseudo-code, but it might guide you a bit. Hope it helps.

How to get the ID of images which are stored in smart phone's library

from the beginning I use
Drawable pic = getResources().getDrawable(R.drawable.android);
to get the drawable object while R.drawable.android means the ID of the picture I put in drawable folder.
However, I would like to get also the drawable object of the images which are stored in library.
do you have any suggestion
I hope you mean by stored in library mean some where in SD card, then use
Drawable pic = Drawable.createFromPath(pathName);
or to get from stream
Drawable pic = Drawable.createFromStream(is, srcName)

how to set imageview src?

I have an image view and a string src. I want to set the imageview source to the string src that I have, but am unable to do so beacuse the method expects an int:
imgview.setImageResource(int);
Since this method takes an int how can I accomplish my goal of using a string?
Each image has a resource-number, which is an integer. Pass this number to "setImageResource" and you should be ok.
Check this link for further information:
http://developer.android.com/guide/topics/resources/accessing-resources.html
e.g.:
imageView.setImageResource(R.drawable.myimage);
To set image cource in imageview you can use any of the following ways. First confirm your image is present in which format.
If you have image in the form of bitmap then use
imageview.setImageBitmap(bm);
If you have image in the form of drawable then use
imageview.setImageDrawable(drawable);
If you have image in your resource example if image is present in drawable folder then use
imageview.setImageResource(R.drawable.image);
If you have path of image then use
imageview.setImageURI(Uri.parse("pathofimage"));
What you are looking for is probably this:
ImageView myImageView;
myImageView = mDialog.findViewById(R.id.image_id);
String src = "imageFileName"
int drawableId = this.getResources().getIdentifier(src, "drawable", context.getPackageName())
popupImageView.setImageResource(drawableId);
Let me know if this was helpful :)

How to Set Background Image from asset folder in android?

I am facing problem while setting the backGround Image of LinearLayout from asset folder.
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
frontTextView.setBackgroundDrawable(d);
Can someone help me out.
First you create a Drawable object from the asset file:
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
and then set it to some View that only supports Drawables as a background.
As far as I'm aware, you cannot access assets directly like you are trying to. You'll need to use the AssetManager class to get at your data if you want to store it as an asset. Here's a pretty decent blog post explaining a bit about resources and assets, though the official documentation is also a good resource, of course.
I'll also add, though, that things like background images are typically best stored in res/drawable and accessed using the R.drawable.* style (the blog post linked above also discusses this) whenever possible. It's not really clear why you need to do it this way from your provided code sample, though, so I suppose that's ultimately your call.
EDIT: added create image from InputStream...
I had the similar problem using ImageButton. I figured it out by loading bitmap from assets and using it as image for ImageButton. Probably not a good approach, but is working and solved my problem - unability to have subfolders in drawable dir and not allowed characters in file names.
(Yes, I can use prefix instead of subdir, and rename files to match the pattern (lowercase only and numbers) and I probably will do it later.)
InputStream is = null;
try {
is = this.getResources().getAssets().open("Images/Fruits/Apple.png");
} catch (IOException e) {
Log.w("EL", e);
}
Bitmap image = BitmapFactory.decodeStream(is);
ImageButton ib2 = (ImageButton) findViewById( R.id.imageButton2);
ib2.setImageBitmap( image);

Categories

Resources