android - How to store image in SQLite database -
in application uploading image gallery , want store image in sqlite database. how store bitmap in database? converting bitmap string , saving in database. while retrieving database, not able assign string imageview since string.
imageupload12 .java:
public class imageupload12 extends activity { button buttonloadimage; imageview targetimage; int = 0; database database = new database(this); string i1; string img; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main5); buttonloadimage = (button) findviewbyid(r.id.loadimage); targetimage = (imageview) findviewbyid(r.id.targetimage); bundle b = getintent().getextras(); if (b != null) { img = b.getstring("image"); targetimage2.setimageuri("image"); //i getting error cant assign string imageview. } buttonloadimage.setonclicklistener(new button.onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub intent intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri); log.i("photo", "" + intent); startactivityforresult(intent, i); = + 1; } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case 0: if (resultcode == result_ok) { uri targeturi = data.getdata(); // texttargeturi.settext(targeturi.tostring()); bitmap bitmap; try { bitmap = bitmapfactory.decodestream(getcontentresolver().openinputstream(targeturi)); targetimage.setimagebitmap(bitmap); i1 = bitmap.tostring(); log.i("firstimage........", "" + i1); targetimage.setvisibility(0); sqlitedatabase db = database.getwritabledatabase(); db.execsql("insert upload values('" + i1 + "');"); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } } break; } } }
image.class:
public class image extends activity { database database = new database(this); static efficientadapter adapter, adapter1; static listview lv1; static sqlitedatabase db; static efficientadapter adp; static cursor c1; static vector < string > image = new vector < string > (); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); db = database.getreadabledatabase(); c1 = db.rawquery("select * upload;", null); if (c1.movetofirst()) { { image.add(c1.getstring(0).tostring()); } while (c1.movetonext()); c1.close(); } lv1 = (listview) findviewbyid(r.id.list); adapter = new efficientadapter(this); lv1.setadapter(adapter); imageview add = (imageview) findviewbyid(r.id.imv1a); add.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub image.clear(); intent = new intent(image.this, imageupload12.class); startactivity(i); } }); } private static class efficientadapter extends baseadapter { // protected final context context = null; protected layoutinflater mlayoutinflater; alertdialog.builder abuilder; public efficientadapter(context context) { // todo auto-generated constructor stub mlayoutinflater = layoutinflater.from(context); } @override public int getcount() { // todo auto-generated method stub return image.size(); } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub final viewholder mvholder; if (convertview == null) { convertview = mlayoutinflater.inflate(r.layout.pjtlistdetails, parent, false); mvholder = new viewholder(); mvholder.t1 = (textview) convertview.findviewbyid(r.id.pjtdetails); mvholder.time = (textview) convertview.findviewbyid(r.id.name); mvholder.imv = (imagebutton) convertview.findviewbyid(r.id.editic); mvholder.imvd = (imageview) convertview.findviewbyid(r.id.delete); mvholder.imvf = (imageview) convertview.findviewbyid(r.id.fwd); mvholder.imv.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string img = image.elementat(position); log.i("image...", "" + img); context ctx = v.getcontext(); intent myintent = new intent(); ctx = v.getcontext(); myintent.setclass(ctx, imageupload12.class); myintent.putextra("image", img); ctx.startactivity(myintent); image.clear(); } }); static class viewholder { imagebutton imv; imageview imvd, imvf; } } } } }
you have use "blob" store image.
ex: store image in db
public void insertimg(int id , bitmap img ) { byte[] data = getbitmapasbytearray(img); // function insertstatement_logo.bindlong(1, id); insertstatement_logo.bindblob(2, data); insertstatement_logo.executeinsert(); insertstatement_logo.clearbindings() ; } public static byte[] getbitmapasbytearray(bitmap bitmap) { bytearrayoutputstream outputstream = new bytearrayoutputstream(); bitmap.compress(compressformat.png, 0, outputstream); return outputstream.tobytearray(); }
to retrieve image db
public bitmap getimage(int i){ string qu = "select img table feedid=" + ; cursor cur = db.rawquery(qu, null); if (cur.movetofirst()){ byte[] imgbyte = cur.getblob(0); cur.close(); return bitmapfactory.decodebytearray(imgbyte, 0, imgbyte.length); } if (cur != null && !cur.isclosed()) { cur.close(); } return null ; }
Comments
Post a Comment