display image from array in android - java

I declared the following array which contains images:
private int[] LEAVES = {
R.drawable.leaf_green,
R.drawable.leaf_red,
R.drawable.leaf_yellow,
R.drawable.leaf_other,
};
If I would want to show one image from the array how can I refer to a specific image? let's say I would want to refer to LEAVES[1]. How can I display it?

Actually setImageResource method sets a drawable as the content of ImageView. It takes id of drawable as argument. Here we have array of drawable ids. we can pick drawable id from array to use. below code shows syntax of of the method.
imageView.setImageResource(LEAVES[1]);

Related

Sending Images Between Activities - Android (Kotlin/Java)

I have a class which encapsulates the information I want to pass over and it is parcelable. So I can pass its properties from one activity to another by using Intent.putExtra() method. But I cannot send images with it since images' sizes are larger than it should be (it doesn't matter whether they are Bitmap or ByteArray).
User downloads images from Firebase Storage and it is very likely that when user views an image, she won't see it again. So saving images in a cache-like storage isn't really necessary.
In Swift, I can do something like this:
// We are in currentViewController which is the activity that will send the image
let someNewViewController = NewViewController() // Create an instance of NewViewController class which is the activity that will receive image
someNewViewController.image = someImage // NewViewController class has a property called image, assign it an image from currentViewController
navigationController.pushViewController(someNewViewController, animated: True) // Go to someNewViewController activity
And then you can easily use image in the someNewViewController by accessing image property. When someNewViewController deallocated, image will be removed by garbage collection.
Is there a similar way in Kotlin/Java? Or is there an alternative to putExtra() that will allow large files to be transferred?
Thanks in advance!

Searching a method like getResourcesID(int)

I'm searching a method that gives me the ID (int) from the Resource from a ImageView in my App. I don't know the name of the Image or something else. i only have an imageview and there is an Image from my Resources inside.
The reason is that i need it is to save it in my SQLite Database as an Integer.
Because later i will read this Data in my SQLite Database to use the method
ImageView.setImageResource((int) R.drawable.SavedPic).
I'm only a beginner in programming so it will not be the best way to do it but i coding the hole day and i hope that you can give me the right method.
There is no direct way to get the Image Resource Id from an ImageView. But there is a way to hold onto that using tags. So if you have an ImageView called imageView and an image resource called R.drawable.image you can do something like this:
imageView.setImageResource(R.drawable.image);
imageView.setTag(R.drawable.image);
And later when you want to get the resource Id associated with the ImageView you can fetch it using
int imageResourceId = (Integer) imageView.getTag();

How to get the uri of the images stored in the phone and return them in a String[]

I am trying to create a custom gallery with a single select option. I've used the code provided by google docs to create a GridView that contains the images contained in drawable.
Now I want to change it by making the GridView show all the images stored in the phone.
To get all images from the phone you need to query the android.provider.MediaStore
First make sure you have the AndroidPermission.READ_EXTERNAL_STORAGE, then get the thumbnails for the grid.
Here is a simple way to do this:
HERE
Hope this helps.

How do I insert multiple image in Edit Text in android

I am trying to insert multiple images in one editText. I have used following code to attach image to EditText
txt = (EditText)findViewById(R.id.text);
Bitmap bitmap = BitmapFactory.decodeFile(attach);
Drawable drawable = new BitmapDrawable(bitmap);
txt.setBackgroundDrawable(drawable);
But the problem is it only attach one file and show, If i used array then only the last image is only show. at any how it only shows one Image. Is there any way to show multiple images in one Editbox. Thanks in advance.
Because you can only have one background.... if you want more then there is a layer Drawable which you can use and also you can put the button in a frame layout and add a couple of imageViews below/over it for the rest of the images.
But the best solution will probably be instead of having a couple of bitmaps to just make a single one in Photoshop or equivalent photo editing app and place that one.
According to docs: http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable(android.graphics.drawable.Drawable)
You are not able to provide more than one Drawable item in argument
And I don't know if you understand what is .setBackgroundDrawable(d) method purpose, but it is not meant to be used for displaying images inside text but to set Background of EditText View.
So you're not inserting images in EditText but setting its background,
look for some Rich Text Edit components usable for Android
such as http://code.google.com/p/android-richtexteditor/
or others..
You can probably use a LayerDrawable with the constructor LayerDrawable(Drawable[] layers) to chain together several images and display them as an EditText's background, though as Marek said, I suspect you're looking for something other than setting images in the background.

Android: Adding a Drawable to an existing LayerDrawable

I have a LayerDrawable which I construct with an array of Drawables of 5 Drawables.
Now let’s say in run-time I want to add another Drawable to my LayerDrawable, in response to an event. How do I do that without having to re-create the LayerDrawable, this time with an array of Drawables of 6 Drawables?
Thanks.
After LayerDrawable is created, new Drawables can not be added to it.
See the source of LayerDrawable: array of drawables is saved in mLayerState.mChildren and is only set in Constructor.
However, setDrawableByLayerId(..) can be used to exchange an existing Drawable with a new one.
You can addLayer since Api level >= 23:
https://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html#addLayer(android.graphics.drawable.Drawable)

Categories