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));
Related
i have an android application that use sqlite and after it fetch the data it must be displayed in a listview.
my question is how to display images?
can anyone help me ???
this my code
//but here it display the same image for the hall list how to display each image in its right place ??
for (int i = 0; i < 49; i++) {
ImageView imagenow = (ImageView) convertView
.findViewById(R.id.imageView1);
// flag1
int imageid = context.getResources().getIdentifier("brazil_flag",
"drawable", context.getPackageName());
imagenow.setImageResource(imageid);
}
Thinking in terms of MVC will help MVC pattern in Android?
Create a Layout with ListView which holds Images in each element
Create an Adapter to read the images from Database
In the Activity, use the Adapter to fill Images in the ListView
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);
^_^
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
My cursor gets 3 columns of data from sqlite, 1st column is recipe name, 2nd column in recipe author and 3rd column is the url for the recipe's image. here's a portion of my code
private static final String fields[] = { "recipeID", "recipeName", "thumbnail"};
Cursor mCursor = dbAdapter.getAllTitles(); //get all titles returns the recipe name, author and thumbs
startManagingCursor(mCursor);
dataSource = new SimpleCursorAdapter(this, R.layout.recipelist, mCursor, fields,
new int[] { R.id.recipeAuthor, R.id.recipeName, R.id.recipeThumb });
setListAdapter(dataSource);
Obviously instead of displaying the recipe image, the code above just displays the url for the recipe image at R.id.recipeThumb. How can I make it display pictures from the internet instead?
The post right below the answer in this discussion might be of use to you:
Lazy load of images in ListView
You need a CustomAdapter and override the getView() method.
In the getView() method based on the Url you create a ImageView.
getView(...) {
//First Create a Drawable from the InputStream of the Url
//store this drawable locally so that you don't have to fetch it again
Drawable imgDrawable = Drawable.createFromStream(inputStream,"");
//set this in the imageView
imgView.setImageDrawable(imgDrawable);
//set this imgView in the contentview and return
}
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);
}