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);
}
Related
I have a form that accepts texts and images and I want to store the data in the same database table. Below is the declaration for my record but they are all strings so how do i convert the images to strings to add to this list:
site_info_record.createSiteInfoRecord(sql_sysaid_id, sql_site_id, sql_link_id, sql_customer_name, sql_site_contact,
sql_task_type, sql_address, sql_region, sql_phone, sql_fax, sql_mobile, sql_email,
sql_landlord_name, sql_rent_status, sql_loc_cords, sql_power_status, sql_voltage, sql_aircon, sql_server_room,
sql_location_idu, sql_earthing, sql_distance_idu, sql_security, sql_ducts, sql_unit, sql_install_type,
sql_isp, sql_radio_type, sql_isp_type, sql_isp_name, sql_snr_rx, sql_bs, sql_remark);
And when i want to retrieve, its also in string format:
public String getInfoData() {
String[] columns = new String[]{ROW_ID, SYSAID_ID, SITE_ID, LINK_ID, CUSTOMER_NAME, SITE_CONTACT, TASK_TYPE, ADDRESS,
REGION, PHONE, FAX, MOBILE, EMAIL, LANDLORD_NAME, RENT_STATUS, LOCATION, POWER_STATUS, VOLTAGE_MEASUREMENT, AIRCON,
SERVER_ROOM_STATUS, LOCATION_IDU, EARTHING, DISTANCE_IDU, SECURITY_CABLES, DUCTS, UNIT_TYPE, INST_TYPE,
HAVE_ISP, RADIO, ISP_TYPE, ISP_NAME, SNR_RX, BS, REMARKS};
How do i fix images data in these
This will slow your database and application. Instead of this you can try to save to SDCard and keep URI at the db.
Very easy
Two options
one:
byte[] bytes = {...}
String str = new String(bytes, "UTF-8");
or
public static String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("LOOK", imageEncoded);
return imageEncoded;
}
public static Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
two:
You can use GSON and make an object from all your text and ByteArray and what ever you want and than just do: GSON.toJson();
In a table , you can have many columns . To have text and images in same table you can have different columns for them with following data types :-
Text - VARCHAR
Images - BLOB
Sqlite is having a data type BLOB which can hold objects .
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
I am developing an application that stores some static images in sqlite and I want to retrieve the same in a imageview.
how do I do this
thanks.
sqlite3 supports the blob type, you can save the bitmap content using the blob type.
However, the blob type has a size limit, and to save to a blob type is difficult.
So, I suggest to save the bitmap local or on the sdcard, and save the path in the database.
added :
table define a column with name 'image' using blob type
Bitmap map = ...;
ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(16*1024);
map.compress(CompressFormat.JPEG, 80, bufferStream);
byte[] bytes = bufferStream.toByteArray();
ContentValues values = new ContentValues();
values.put("image", bytes);
convert the image byte array, using SQLiteDatabase class method or content provider as you like:
public long insert (String table, String nullColumnHack, ContentValues values)
to insert to table, so it save the image to blob.
and then: when you query the blob data, you can create the image like this:
BitmapFactory.Options option2 = new BitmapFactory.Options();
option2.inPreferredConfig = Bitmap.Config.RGB_565;
// added for reducing the memory
option2.inDither = false;
option2.inPurgeable = true;
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, option2);
hope it can implement your request. -):
Friend's,
I stored image url has a byte array in database has blob type and at time of fetching this image i'm getting nullpointer exception.how can i fix this problem,here the code for fetching data from db...
String bb = c.getString(c.getColumnIndex(JR_Constants.IMAGE_97));
Log.v(TAG,bb);
Bitmap theImage = BitmapFactory.decodeByteArray(bb, 0, bb.length);
myImage.setImageBitmap(theImage);
what i'm doing mistake here.
You need to convert the String to a ByteArray. Basically just do bb.getBytes() instead of passing bb
String bb = c.getString(c.getColumnIndex(JR_Constants.IMAGE_97));
Log.v(TAG,bb);
Bitmap theImage = BitmapFactory.decodeByteArray(bb.getBytes(), 0, bb.length());
myImage.setImageBitmap(theImage);
I just want to note that bb.length should be bb.length()