android - read and save images into SQLite database -


hey guys working on android retrieve , save images sqlite database . here code ==>>

     public class mainactivity extends activity implements onclicklistener {             protected static textview textview;              protected static imageview image1, image2;            protected button get_image, save_image, read_image;            private string selectedimagepath;            private static final int select_picture = 1;            string db_name = environment.getexternalstoragedirectory() + "/test.db";               string table_name = "mytable";      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          image1 = (imageview) findviewbyid(r.id.imageview1);         image2 = (imageview) findviewbyid(r.id.imageview2);         textview = (textview) findviewbyid(r.id.textview1);          get_image = (button) findviewbyid(r.id.get_image);         get_image.setonclicklistener(this);          save_image = (button) findviewbyid(r.id.save_image);         save_image.setonclicklistener(this);          read_image = (button) findviewbyid(r.id.read_image);         read_image.setonclicklistener(this);      }      public void onclick(view v) {          int id = v.getid();         switch (id) {          case r.id.get_image:             intent intent = new intent();             intent.settype("image/*");             intent.setaction(intent.action_get_content);             startactivityforresult(                     intent.createchooser(intent, "select picture"),                     select_picture);             break;          case r.id.save_image:             createtable();             saveindb();             break;          case r.id.read_image:             readfromdb();             break;         default:             break;          }     }      public void onactivityresult(int requestcode, int resultcode, intent data) {         if (resultcode == result_ok) {             if (requestcode == select_picture) {                 uri selectedimageuri = data.getdata();                 selectedimagepath = getpath(selectedimageuri);                 system.out.println("image path : " + selectedimagepath);                 image1.setvisibility(view.visible);                 image1.setimageuri(selectedimageuri);             }         }     }      @suppresswarnings("deprecation")     public string getpath(uri uri) {         string[] projection = { mediastore.images.media.data };         cursor cursor = managedquery(uri, projection, null, null, null);         int column_index = cursor                 .getcolumnindexorthrow(mediastore.images.media.data);         cursor.movetofirst();         return cursor.getstring(column_index);     }      void createtable() {         sqlitedatabase mydb = openorcreatedatabase(db_name,                 context.mode_private, null);         string mysql = "create table if not exists "                 + table_name                 + " (_id integer primary key autoincrement, name text not null, image blob);";         mydb.execsql(mysql);         mydb.close();     }      void saveindb() {         sqlitedatabase mydb = openorcreatedatabase(db_name,                 context.mode_private, null);         byte[] byteimage1 = null;         string s = mydb.getpath();          mydb.execsql("delete " + table_name);          // clearing table         contentvalues newvalues = new contentvalues();         string name = "coderzheaven";         newvalues.put("name", name);         try {             fileinputstream instream = new fileinputstream(selectedimagepath);             bufferedinputstream bif = new bufferedinputstream(instream);             byteimage1 = new byte[bif.available()];             bif.read(byteimage1);             newvalues.put("image", byteimage1);             long ret = mydb.insert(table_name, null, newvalues);             if (ret < 0)                 textview.append("error");         } catch (ioexception e) {             textview.append("error exception : " + e.getmessage());         }         mydb.close();         textview.append("\n saving details \n name : " + name);         textview.append("\n image size : " + byteimage1.length + " kb");         textview.append("\n saved in db : " + s + "\n");         toast.maketext(this.getbasecontext(),                 "image saved in db successfully.", toast.length_short).show();     }      void readfromdb() {         byte[] byteimage2 = null;         sqlitedatabase mydb;         mydb = openorcreatedatabase(db_name, context.mode_private, null);         cursor cur = mydb.query(table_name, null, null, null, null, null, null);         cur.movetofirst();         while (cur.isafterlast() == false) {             textview.append("\n reading details \n name : " + cur.getstring(1));             cur.movetonext();         }          // /////read data blob field////////////////////         cur.movetofirst();         byteimage2 = cur.getblob(cur.getcolumnindex("image"));         setimage(byteimage2);         cur.close();         mydb.close();         toast.maketext(this.getbasecontext(),                 "image read db successfully.", toast.length_short).show();         toast.maketext(this.getbasecontext(),                 "if image big, please scrolldown see result.",                 toast.length_short).show();     }      void setimage(byte[] byteimage2) {         image2.setimagebitmap(bitmapfactory.decodebytearray(byteimage2, 0,                 byteimage2.length));         textview.append("\n image size : " + byteimage2.length + " kb");     }  }   

the problem facing when ever trying execute above code not able retrieve or save image from/into sqlite database. able fetch images gallery. stuck, please me.

thank :)

i have changed db store location.. works.

try this...

 sqlitedatabase mydb = mainactivity.this.openorcreatedatabase("test.db",//your db file name             mainactivity.this.mode_private, null); 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -