Change ImageView dynamically knowing file name from database - android

In my application I am getting the filename dynamically from database but I'm not sure how to update the image of ImageView.
Let's say I have this code:
String str = "image2.jpg";
ImageView imageView = (ImageView) findViewById(R.id.image);
How could I set the image to be image2.jpg, if current image is another?
PS. In my application, I have a spinner. Every time the selected element is changed, I make a database request and I get the image name, and I need to modify image of imageview dynamically.

write a method like this in your Activity
public Drawable getImage(String name) {
return getResources().getDrawable(
getResources().getIdentifier(name, "drawable",
getPackageName()));
}
pass you image name here you will get drawable object then set it to Imageview
Drawable image = getImage(name);
imageView.setImageDrawable(image);

Related

How to change an ImageView with a filename

I have a string with the name of the file that I want (R.drawable.square) to put in the imageview.
String shape = "square"
I am trying to use the method below to show the image
imageView.setImageResource()
Hardcoding the filename in works but I want to be able to pass different filenames in.
image.setImageResource(R.drawable.square)
tl;dr
can't get from String:"square" to int:R.drawable.square
Please see Android, reference things in R.drawable. using variables?
//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 );
I achieved the effect by putting the images in the assets directory
I think it's better to use you Strings as key and your resource ids as value in a Map Object.
Map<String, Integer> resources = new HashMap<>();
resources.put("square", R.drawable.square);
// resources.put("...", R.drawable....);
imageView.setImageResource(resources.get("square"));

Android SharedPreferences get drawable if value doesn´t exist

so in my app I need to save and to display an image on a image view, so i use SharedPreferences for saving it, the problem is that, if there is not a saved value, i want to display an image from drawables, and for that I´m using this code:
final ImageButton imgView = (ImageButton) findViewById(R.id.UserImageButton);
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString("Image", String.valueOf(getResources().getDrawable(R.drawable.user, getApplicationContext().getTheme())));//NameOfTheString, StringIfNothingFound
imgView.setImageBitmap(BitmapFactory.decodeFile(photo));
But if there is no image saved, the drawable is not used on the ImageView, and it doesn´t show anything.
How could I fix it?
There's no requirement to use the default value of your getString() method as a direct path to your drawable. You could simply do the following:
private static final String IMAGE_PREF = "image_pref_key";
private static final String PREF_FAIL_CODE= "no_image_found";
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString(IMAGE_PREF, PREF_FAIL_CODE);
if (photo.equals(PREF_FAIL_CODE) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
}
When you are getting the string from default shared preferences, if the value is not stored, set the default value returned to something e.g. "not found"
Now before setting doing setImageBitmap check if the value in your String variable photo is "not found". If it is display the drawable from your resource. Else display your saved image after decoding it as your are doing it currently.

Setting ImageView source to an image from SQLite

I have a small SQLite DB in my project, where I have table "Products". One of the columns of this table is String "image". In the activity I receive a product id in the Intent extras, and create a Product object from the DB. In the layout of this activity I have an ImageView. Each time a Product object is created, I want to set the ImageView image to the one that is stored in the object. I tried to do the following:
ImageView product_image = (ImageView) findViewById(R.id.product_image);
product_image.setImageDrawable(Integer.parseInt("R.drawable."+productToPresent.getImage());
productToPresent is my Product object and getImage returns the image string name.
However at this point my app crashes and I'm getting the following error message in the logcat:
RuntimeException: Unable to start activity ComponentInfo{com.myapp.appname/com.myapp.appname.ActivityProductDetails}: java.lang.NumberFormatException: Invalid int: "R.drawable.cat02_prod02"
where cat02_prod02 is the string stored stored in my DB and the filename of the image.
So how can it be done?
You can do it in this manner:
int id = getResources()
.getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
imageview.setImageResource(id);

How to set the image from drawable folder to imageview using the path of the image from sqlite database?

anyone knows how to set the image from drawable folder to imageview using the path of the image from sqlite database?
Any help will be appreciated. Sample codes will be helpful. Thanks. =)
ImageView imageView = (ImageView) findViewById(R.id.imageView);
String test = "imageName"; // store only image name in database(means not store "R.drawable.image") like "image".
int id = getResources().getIdentifier("projectPackageName:drawable/" + test, null, null);
imageView.setImageResource(id);
You can get Bitmap object from file,and use it to set your image view.
Here code:
String img_path = get from your database
...
Bitmap bmp = BitmapFactory.decodeFile(img_path);
//use it.
yourimgview.setImageBitmap(bmp);
^_^

Displaying images in android?

I am working on student details application. I stored all the student details in sqlite database and I kept my sqlite file in assets folder. I need to display the student photo also. So for that I kept a column in db and stored the image names of the students. Now I am displaying the students names in a listview. When the student name is clicked it has to display all the details of that particular student along with the photo. How can I display a particular student photo? I kept the student photos in drawable folder.
My Code:
student.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/yes"
android:src="#drawable/photo"/>
Student.java
Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.photo);
ImageView Image1 = (ImageView) findViewById(R.id.yes);
Image1.setImageBitmap(image );
Please help me regarding this....thanks in advance
You should store student photo id (drawable ID) in DB.
when user click on student name on List view get that student photo drawable ID from DB.
OR
store every student Photo drawable id in Listview li as Invisible get That ID when that Li clicked.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/yes"
android:src="#drawable/photo"/>
Drawable d = getResources().getDrawable(R.drawable.photo);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
ImageView Image1 = (ImageView) findViewById(R.id.yes);
Image1.setImageBitmap(bitmap);
I kept the student photos in drawable folder.
Set Background Drawable to your Imageview.
ImageView im =(ImageView)findViewById(R.id.imageview);
im.setBackgroundDrawable(R.drawable.photo);
As i understood You have all the student details in sqlite file and student name in drawable folder then you can do the following
1. give a unique id for student
2. and refer the same id for image name
then access the student detail from Sqlite file and then display corresponding images. and you can make array of drawable resources for easy reference.
fetch student image name from sqlite and after that just do this way
where StudentImageName is String
int resID = getResources().getIdentifier("Your Package Name:drawable/" + StudentImageName, null, null);
ImageView Image1 = (ImageView) findViewById(R.id.yes);
Image1.setImageResource(resID);
and check image is present in drawable before set to imageview
either you can also try
int resID = getResources().getIdentifier("Your Package Name:drawable/" + StudentImageName, null, null);
Bitmap image = BitmapFactory.decodeResource(getResources(),resID);
ImageView Image1 = (ImageView) findViewById(R.id.yes);
Image1.setImageBitmap(image)
Include the photo when you insert student data in your database.
You can do this by converting the Bitmap into a byte array.
Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
(Your image column should accept BLOB as field type)
Then whenever you want to display the image once again retrieve the byte array from the database then convert it to Bitmap so you can display it inside an ImageView
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);
You can check this link for more info on
'How to insert image data to sqlite database in Android and how to retrieve it back'
http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

Categories

Resources