I am reading data of employees from excel file stored in internal storage, unfortunately the app is totally offline and so have offline images , which i need to store in drawable folder.
Now the question i have is ;
There are 350 images named as employee id , for example
1.jpg of emp id = 1 , 2.jpg of emp id = 2.
I am using RecyclerView , in that onBindViewHolder method i have to set images according to the employee id.
Please guide me to show the image into imageView.
Please comment for more input from my side.
Plz dont mark it negative if you can't answer.
You could try something like this.
Example if trying to get employee 1526 the drawable would be R.drawable.1526
String id = employee.getId(); //1526
int imageId = context.getResources()
.getIdentifier(id, "drawable", context.getPackageName());
id being a string.
then with the image view
imageView.setImageResource(imageId);
Related
is it possible to avoid storing images in a database and putting them into the drawable or mipmap folders instead? In the database then I would like to put only the reference to ressources and call it like this:
String image = "R.mipmap.image1"; //This will be the data retrieved from Database
int imageInt = Integer.parseInt(image);
imgview.setImageRessource(imageInt);
Actually I want to use this in a RecyclerView so I simplified the example code here but this is showing me the following error
java.lang.NumberFormatException: Invalid int: "R.mipmap.image1"
Thanks
One simple way is to store the image name in database (i.e. "image1" in your example). Then later get the identifier for it using image name as below:
int imageId=context.getResources().getIdentifier("imageName","mipmap",context.getPackageName());
Now, you can use this imageId for setting image to image view.
imgview.setImageRessource(imageId);
I returned a string from SQLite table and want to load image from the drawable folder. Is it possible?
For example :
String name = sql.getName(1); //name is now Barney
img.setImageResource(R.drawable.name);
Is it possible to load it this way?
I've tried.
int imageid = getResources().getIdentifier("com.tes.test.main:drawable/" + name.toLowerCase(), null, null);
img.setImageResource(imageid);
Doesn't work either. :(
Any help is appreciated. Thanks.
1st. A few weeks ago i used this for a VideoView. Must be nearly the same for pictures.
In my place i stored the videos in the /raw folder.
mVideoView.setVideoPath("android.resource://your-package-name/raw/"+videoName);
2nd. Why don't you save the picture into the database and read it from there?
like this:
byte[] blob = cursor.getBlob(2);
Ah, I've found it.
String image = "test";
int pimageid = getResources().getIdentifier("com.yourpackage.name:drawable/"+image,null,null);
img.setImageResource(pimageid);
I already stored images into array (int image[0][0]=R.Drawable.img1 like this). I want to compare the image[0][0] with which image is in the imageview.
I want to know which image is in the imageview if i click the imageview, the image will be change. so i need to know which image is in imageview?
I'd recommend using the tag of the ImageView for this.
Whenever you set the ImageView's image, also set the tag, and then later check the tag to see what's the image.
image.setImageResource(R.drawable.pic1);
image.setTag(R.drawable.pic1);
...
long id = (long) image.getTag();
if (id == R.drawable.pic1) {
...
}
You can store the id of the current drawable using View.setTag(). Later you can retrieve by View.getTag() and compare it with the values stored in your array.
In one activity, there are 1 text view and 1 image view. I have to display number of data for different images. I have stored the data in string-array in strings.xml. My code is working fine for them. Problem is with images. I have stored images in R.raw folder. How can I get the images in image view so that for every data corresponding images can be displayed.
In the listactivity(where the data are displayed in a listview i have added async task and in doinbackground i am loading values from these arrays ). I am stuck in images. How to do that? pls help...
started like this,
int image[]= {getResources().getIdentifier("aa", "raw", getPackageName()),
getResources().getIdentifier("bb", "raw", getPackageName()),
getResources().getIdentifier("cc", "raw", getPackageName()),};
Pls reply..
Many Thanks
Why are the images in Raw? Put them into /drawble-hdpi or /drawable-mdpi...whatever and then set the image by myImageView.setImageResource(R.drawable.myimage) and then associate it with your data however you want...
For resources in your raw folder, you should be able to use Bitmap.decodeResource to obtain an image you can pass to the ImageView.
A bit wierd problem:
Is it possible to get back id or/and a name of active resource in an ImageView? That's the code.
ImageView photo = (ImageView) findViewById(R.id.placeForPhoto);
//here I choose random photo from drawable (photo 1 to photo 10)
//and then I show it on the screen as below
photo.setImageResource(R.drawable.photo1); //or photo2 or photo3 or...
In the other part of the code I would like to check which photo is atm presented in placeForPhoto. If it's R.drawable.photo1 or R.drawable.photo2 or...
So what I need is just smth like to have a possibility to achieve such a comparison:
if ([presented atm photo in ImagView photo] == R.drawable.photo1)
{do something}
else if ([presented atm photo in ImagView photo] == R.drawable.photo2)
{do smth different}
else {...}
I tried:
photo.getID() but it returns ID of placeForPhoto not for the resource inside;
photo.getResource() but it returns some other value different than id of photo
getResource().getIdentifier() but it gives id when you have the name and I don't have the name (or maybe it's possible to get the name from ImageView photo?)
getResources().getResourceName() but then I have to have id which I'm just looking for.
Thx in advance.
Is it possible to get back id or/and a name of active resource in an ImageView?
No, because an ImageView does not have to hold a resource. It could hold a bitmap obtained by other means (e.g., download from the Web).
So what I need is just smth like to have a possibility to achieve such a comparison
Store the ID in a data member of the activity, or in the tag of the ImageView (i.e., setTag() and getTag()).