java - How to access SQLite Database from another class -


i'm need access sqlite database created in 1 class other activity. want access public void getimage(view view) {but app crashes when use it. here activity:

public class cameratodatabase extends activity {     private static final int camera_request = 1888;     public imageview imageview1;     imagehelper = new imagehelper(this);      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_camera_to_database);           button b = (button) this.findviewbyid(r.id.camera);         b.setonclicklistener(new view.onclicklistener() {              public void onclick(view v) {                 intent cameraintent = new intent(                         android.provider.mediastore.action_image_capture);                 startactivityforresult(cameraintent, camera_request);             }         });     }      protected void onactivityresult(int requestcode, int resultcode, intent data) {         if (requestcode == camera_request) {             bitmap photo = (bitmap) data.getextras().get("data");             bytearrayoutputstream stream = new bytearrayoutputstream();             photo.compress(bitmap.compressformat.jpeg, 100, stream);             byte[] bytearray = stream.tobytearray();             help.insert(bytearray);         }     }      public void getimage(view view) {          cursor c = help.getall();         if (c.movetonext())         {             byte[] bytearray = c.getblob(0);             bitmap bmp = bitmapfactory.decodebytearray(bytearray, 0, bytearray.length);             imageview1.setimagebitmap(bmp);          }         c.close();      } 

and here create database:

public class imagehelper extends sqliteopenhelper {     private static final string database_name = "clothes.db";     private static final int schema_version = 3;      public imagehelper(context context) {         super(context, database_name, null, schema_version);     }      @override     public void oncreate(sqlitedatabase db) {         // todo auto-generated method stub         db.execsql("create table image(_id integer primary key autoincrement,imageblob blob);");     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     }      public void insert(byte[] bytes) {         contentvalues cv = new contentvalues();          cv.put("imageblob", bytes);         log.e("inserted", "inserted");         getwritabledatabase().insert("image", "imageblob", cv);      }      public cursor getall() {         return (getreadabledatabase().rawquery("select column1 image", null));     }    }  } 

now mainactivity shouldlike that:

package com.solution.sa.stack_camerasql;  import android.content.intent; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.imageview;  import java.io.bytearrayoutputstream;  public class mainactivity extends appcompatactivity {      private static final int camera_request = 1888;     public imageview imageview1;     imagehelper ;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         = new imagehelper(this);         imageview1= (imageview) this.findviewbyid(r.id.myimg);         button b = (button) this.findviewbyid(r.id.camera);         b.setonclicklistener(new view.onclicklistener() {              public void onclick(view v) {                 intent cameraintent = new intent(                         android.provider.mediastore.action_image_capture);                 startactivityforresult(cameraintent, camera_request);             }         });     }      protected void onactivityresult(int requestcode, int resultcode, intent data) {         if (requestcode == camera_request) {             bitmap photo = (bitmap) data.getextras().get("data");             bytearrayoutputstream stream = new bytearrayoutputstream();             photo.compress(bitmap.compressformat.jpeg, 100, stream);             byte[] bytearray = stream.tobytearray();             help.insert(bytearray);             getimage();         }     }      public void getimage() {          cursor c = help.getall();         if (c.movetonext())         {             byte[] bytearray = c.getblob(0);             bitmap bmp = bitmapfactory.decodebytearray(bytearray, 0, bytearray.length);             imageview1.setimagebitmap(bmp);           }         c.close();      } } 

and imgaehelper class :

package com.solution.sa.stack_camerasql;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log;  /**  * created asim on 10/27/2015.  */ public class imagehelper extends sqliteopenhelper {     private static final string database_name = "clothes.db";     private static final int schema_version = 3;      public imagehelper(context context) {         super(context, database_name, null, schema_version);     }      @override     public void oncreate(sqlitedatabase db) {         // todo auto-generated method stub         db.execsql("create table image(_id integer primary key autoincrement,imageblob blob);");     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     }      public void insert(byte[] bytes) {         contentvalues cv = new contentvalues();          cv.put("imageblob", bytes);         log.e("inserted", "inserted");         getwritabledatabase().insert("image", "imageblob", cv);      }      public cursor getall() {         return (getreadabledatabase().rawquery("select  imageblob image", null));     }    } 

now have manage show last captured image instead of loop on images


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -