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);
Related
Sorry, I'm still a beginner in coding, but how do I make this code work? actor art is a variable I have from an array.
And it's the name of a image I have in my drawable. This line of code works on any activity type but not on fragments. How do I set image from a variable from an array?
int imageId = AppUtil.getImageIdFromDrawable(this, actorart);
ImageView ivCoverArt = view.findViewById(R.id.imgactor);
ivCoverArt.setImageResource(imageId);
The error for this is that this cannot be used.
This also doesn't work:
ImageView imageView= view.findViewById(R.id.imgactor);
imageView.setImageResource(actorart);
The error here is that actorart is a String but setImageResource is an action for an int.
You are correct, setImageResource() takes in an integer, more specifially the ID of the resource.
To get the ID, you can use the following:
int id = getResources().getIdentifier("yourpackagename:drawable/" + actorart, null, null);
this cannot be used cause the instance you are refering with this is a Fragment, and I'm pretty sure you need a Context parameter.
To fix it get a valid Context:
int imageId = AppUtil.getImageIdFromDrawable(getActivity(), actorart);
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.
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);
I have a image in res folder like this
I want to access this one dynamically like this
holder.Viewcover.setImageDrawable(Drawable.createFromPath("R.id." + CoverimgUrl.get(position)));
CoverimgUrl is a list which have two image name one is book_cover & and another is blank_image this array is generated dynamically so how can I set this image from that list
In One word how to access a image dynamically which is in drawable folder and I need get that image name from an array list ?
Resources res = getResources();
String mDrawableName = "image_name";
int resourceId = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resourceId);
icon.setImageDrawable(drawable );
First Make CoverimgUrl list of integer
List<Integer> CoverimgUrl =new ArrayList<Integer>();
CoverimgUrl.add(R.drawable.book_cover);
CoverimgUrl.add(R.drawable.blank_image);
Then
holder.Viewcover.setImageResource(CoverimgUrl.get(position));
createFromPath expects a path to the file, not it's ID.
You can use the following:
int id = getResources().getIdentifier(CoverimgUrl.get(position), "id", getPackageName());
holder.Viewcover.setImageDrawable(getResources().getDrawable(id));
getIdentifier() gets the ID from the string. When you use the "R" class, it contains static integers for ids. So R.id.some_name is actually an integer, which is the id of the some_name resource.
once you get this integer with getIdentifier, you can use getResources().getDrawable() to get the drawable with the given ID.
Let me know if this works.
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