Displaying images in android? - 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

Related

Change ImageView dynamically knowing file name from database

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);

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);
^_^

How to display images from SQLite database to gridview using Android application

I have images in my database and I have converted them into BLOB but I can't display them on a gridview or listview with other information like this: price, QTY, Description, image
Something like this in your cursor adapter:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));

Displaying image from the sqlite database using cursor in android

i want to display image in imageview from sqlite database using cursor. i used below code to retrieve image but i am not able to display image in imageview.
Cursor c=this.db.query(TABLE_NAME, new String[] { "name","price","image" },
null, null, null, null, null);
name.setText(c.getString(0));
price.setText(c.getString(1));
byte b[]=c.getBlob(2);
Bitmap bp=BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image=(ImageView)findViewById(R.id.ImageView);
//ByteArrayInputStream imageStream = new ByteArrayInputStream(b);
//Bitmap theImage = BitmapFactory.decodeStream(imageStream);
//image.setImageBitmap(theImage);
image.setImageBitmap(bp);
other than image, name and price will display but image will not display.
any suggestion or code that will help me to solve this problem.
Please help me.
Thanks in advance..
I've tried to use Stream instead of byte array, mine example simply works for me.
Also try to use Cursor.getColumnIndex()
byte[] blob = c.getBlob(c.getColumnIndex(YourDB.IMAGE));
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Here's my "create table" statement, pls double check yours
create table datatable(_id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB, price INTEGER);
First of all please did not store the images in database because sqlite will not support more then 1 mb,i may be wrong, images should be store in assets or drawble folder and the name of images should be store in database then you can get it from name to resource conversion by code
String videoFileName= c.getString(c.getColumnIndex(TB_SETTING_VIDEOFILENAME));
int dot = videoFileName.lastIndexOf(".");
videoFileName=videoFileName.substring(0,dot);
Log.d("VIDEOFILENAME", videoFileName);
int videoFileRId = getResources().getIdentifier(videoFileName , "raw", getPackageName());
I hope this will help you.
try this
cursor object; // your cursor
mImageView.setImageBitmap(getImageFromBLOB(object.getBlob(object.getColumnIndex("book_thumb"))));
public static Bitmap getImageFromBLOB(byte[] mBlob)
{
byte[] bb = mBlob;
return BitmapFactory.decodeByteArray(bb, 0, bb.length);
}

Categories

Resources