Reputation: 75
I have 8 different plant species images under assets folder, I cached the 8 images assets filename (corresponding to the path, E.g foxtail.png, orchid.png) in the assets directory in a database. (Plus other information)
I'm 'displaying the 8 plants in a RecyclerView. Clicking on any of the plants opens the Detail Activity. (Passing the image filename as saved in the asset folder E.g foxtail.png)
How do i pick the specific image file in the assets folder that matches the file name that was passed to Detail Activity and set it to an ImageView??
Upvotes: 1
Views: 3864
Reputation: 2128
You can:
Open the file as a stream
InputStream imageStream = null;
try {
// get input stream
imageStream = getAssets().open("foxtail.png");
// load image as Drawable
Drawable drawable= Drawable.createFromStream(imageStream, null);
// set image to ImageView
image.setImageDrawable(drawable);
}
catch(IOException ex) {
return;
}
Finally remember to close the stream with
if(imageStream !=null){
imageStream.close();
}
or
moving your images in the res/drawable folder you can load the images with:
String yourImageName = getImageNameFromDB();
int resId= getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(resId);
or
with something like this (always with the images into res/drawable):
private enum Plant {
foxtail, orchid, xyz;
}
String value = getPlantFromDB();
Plant plant = Plant.valueOf(value); // surround with try/catch
switch(plant) {
case foxtail :
resId= R.drawable.foxtail
break;
case orchid :
resId= R.drawable.orchid
break;
default :
resId= R.drawable.xyz
break;
Drawable drawable = getResources().getDrawable(resId);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(drawable);
Upvotes: 5
Reputation: 158
Set a tag on each ImageView/View using the Resource Drawable Id eg. R.drawable.foxtail
eg. imageView.setTag(R.drawable.foxtail)
OR view.setTag(R.drawable.foxtail)
When one is selected, get the tag and send it to the next activity:
Then check again eg.
imageTag = getIntent().getIntExtra("chosenPlant");
if ( imageTag == R.drawable.foxtail ){
//Perform action if this pic was selected (foxtail.png)
newImageView.setImageResource(R.drawable.foxtail);
} else ...
Upvotes: 0
Reputation: 494
You could create an array of int containing the resources ids.
int images[]={
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4,
R.drawable.image_5,
R.drawable.image_6,
R.drawable.image_7,
R.drawable.image_8
};
In your database store image ids corresponding to the image positions in the resources array.
| id | image_id | information |
-------------------------------
| 0 | 2 | info_0 |
| 1 | 0 | info_1 |
| 2 | 4 | info_2 |
So when you fetch rows from database, you can use image_id to retrieve the corresponding image from the array
ImageView imageView = (ImageView)v.findViewById(R.id.imageView);
imageView.setImageResource(images[image_id]);
Upvotes: 0