Fetching image from SQLite database in android -


i unable fetch image database, please @ databasehelper (that fetchimg(string i) , mainactivity (that showmeth(view w)) class.

what doing here firstly capturing image camera, temporary pasted on 1st imageview , after click on save button gets saved in database(no problem yet). when click show button must show image in 2nd imageview taking reference id textview's id not showing image, showmeth(view w) unable fetch image database.

my mainactivity

package com.example.expnewbutton;  import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.app.activity; import android.content.intent; import android.content.pm.packagemanager; import android.graphics.bitmap; import android.view.menu; import android.view.view; import android.widget.imageview; import android.widget.textview; import android.widget.toast;  public class mainactivity extends activity {     private uri fileuri;     bitmap img;     textview t1;     databasehelper helper;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         t1 = (textview) findviewbyid(r.id.text);         helper = new databasehelper(this);     }      public void cammethod(view w) {         try {             packagemanager packagemanager = getpackagemanager();             boolean doeshavecamera = packagemanager                     .hassystemfeature(packagemanager.feature_camera);              if (doeshavecamera) {                 // start image capture intent                 intent intent = new intent(mediastore.action_image_capture);                 // our fileuri                 // fileuri = getoutputmediafile();                  intent.putextra(mediastore.extra_output, fileuri);                 startactivityforresult(intent, 100);              }         } catch (exception ex) {             toast.maketext(getapplicationcontext(),                     "there error camera.", toast.length_long)                     .show();         }     }      protected void onactivityresult(int requestcode, int resultcode,             intent intent) {         if (requestcode == 100) {             if (resultcode == result_ok) {                 if (intent == null) {                     // picture taken not returned                     toast.maketext(                             getapplicationcontext(),                             "the picture taken , located here: "                                     + fileuri.tostring(), toast.length_long)                             .show();                 } else {                     // picture returned                     bundle extras = intent.getextras();                     img = (bitmap) extras.get("data");                     imageview imageview1 = (imageview) findviewbyid(r.id.imageview1);                     imageview1.setimagebitmap((bitmap) extras.get("data"));                 }             }         }      }      public void insertimg(view w) {         long = 0;         string id = t1.gettext().tostring();         try {             helper.deleterecord(id);             = helper.insert(id, img);              if (a >= 1) {                 toast.maketext(getbasecontext(),                         + "record saved", 30).show();             } else {                  toast.maketext(getbasecontext(), "not saved", 30).show();              }         } catch (exception e) {             toast.maketext(getbasecontext(), "there error", 5).show();         }     }      public void showmeth(view w) {         string = null;         boolean flag = false;          = t1.gettext().tostring();         try {             flag = helper.fetchimg(i);         } catch (exception e) {             toast.maketext(getbasecontext(), "database exception", 10).show();         }          if (flag == true) {             toast.maketext(getbasecontext(),                     "here image save on imageview", 10).show();             imageview imageview2 = (imageview) findviewbyid(r.id.imageview2);             imageview2.setimagebitmap(helper.bmp);         } else {             toast.maketext(getbasecontext(), "add pic first", 50).show();         }      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.activity_main, menu);         return true;     }  } 

my database file:

package com.example.expnewbutton;  import java.io.bytearrayoutputstream; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.graphics.bitmap; import android.graphics.bitmap.compressformat; import android.graphics.bitmapfactory; import android.util.log;  public class databasehelper extends sqliteopenhelper {      final static string databasename = "image";     final static int databaseversion = 1;     bitmap bmp = null;      public databasehelper(context ctx) {         super(ctx, databasename, null, databaseversion);      }      @override     public void oncreate(sqlitedatabase db) {         try {              log.d("tag4545", "database");             db.execsql("create table mypic(id text,pic blob)");          } catch (sqlexception e) {             e.printstacktrace();         }      }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {          db.execsql("drop table if exists mypic");         // db.execsql("drop table if exists emer");         oncreate(db);      }      public long insert(string id, bitmap img) {          sqlitedatabase base = getwritabledatabase();         byte[] data = getbitmapasbytearray(img); // function         contentvalues value = new contentvalues();         value.put("id", id);         value.put("pic", data);          long = base.insert("mypic", null, value);          return a;     }      public static byte[] getbitmapasbytearray(bitmap bitmap) {         bytearrayoutputstream outputstream = new bytearrayoutputstream();         bitmap.compress(compressformat.png, 0, outputstream);         return outputstream.tobytearray();     }      public boolean fetchimg(string i) {          sqlitedatabase base = getreadabledatabase();         cursor cs = null;         try {             cs = base.query("mypic", new string[] { "pic" }, "id=?",                     new string[] { "i" }, null, null, null);              /*              * string qu= "select pic mypic id"+i;              *               * cursor cs=null; try{ cs =base.rawquery(qu, null); }              * catch(exception e) { return false; }              */             if (cs != null) {                 cs.movetofirst();                 byte[] imgbyte = cs.getblob(0);                 cs.close();                 bmp = bitmapfactory.decodebytearray(imgbyte, 0, imgbyte.length);                 return true;             }              else {                 return false;              }         }          catch (exception e) {             return false;         }     }      public void deleterecord(string pe_id) {         sqlitedatabase base = getwritabledatabase();         base.delete("mypic", "id=?", new string[] { pe_id });     }  } 

my xml file:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/gb2"     tools:context=".mainactivity" >      <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/edittext2"         android:layout_marginleft="16dp"         android:layout_margintop="32dp"         android:layout_torightof="@+id/textview2"         android:onclick="cammethod"         android:text="cam" />      <imageview         android:id="@+id/imageview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />      <button         android:id="@+id/button2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbottom="@+id/button1"         android:layout_marginleft="46dp"         android:layout_torightof="@+id/button1"         android:onclick="insertimg"         android:text="save" />      <textview         android:id="@+id/text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbottom="@+id/button2"         android:layout_alignparentright="true"         android:layout_marginright="22dp"         android:text="photo"         android:textappearance="?android:attr/textappearancelarge" />      <button         android:id="@+id/show"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/text"         android:layout_torightof="@id/imageview1"         android:onclick="showmeth"         android:text="show" />      <imageview         android:id="@+id/imageview2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/imageview1" />  </relativelayout> 

please me in code, need soon. thank you.

so problem may database query(i tried query both methods hidden or not hidden) or may using blob in code. please me find error

you passing string "i" need pass value of i.

you should change query to:

    cs = base.query("mypic", new string[] { "pic" }, "id=?",             new string[] { string.valueof(i) }, null, null, null); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -