android - How can I insert an image into my database? -


so, have database stores recipes. right now, recipe table holds data name, type, etc. but, since want show picture of each recipe in app, need somehow store images database don't know how.

heres have far

public class dbhandler extends sqliteopenhelper{  //    private static class databasehelper extends sqliteopenhelper {          public static final string column_rowid = "_id";         public static final string column_name = "name";         public static final string column_type = "type";         public static final string column_ingred = "ingred";          private static final string tag = "dbhandler";         //private databasehelper mdbhelper;         //private sqlitedatabase mdb;          private static final string database_name = "recipes";         private static final string sqlite_table = "table_recipes";         private static final int database_version = 1;          //private final context mctx;     public dbhandler(context context, string name, sqlitedatabase.cursorfactory factory, int version) {         super(context, database_name, null, database_version);     } /*        databasehelper(context context) {             super(context, database_name, null, database_version);         }*/          @override         public void oncreate(sqlitedatabase db) {             string database_create =                     "create table if not exists " + sqlite_table + " (" +                             column_rowid + " integer primary key autoincrement," +                             column_name + "," +                             column_type + "," +                             column_ingred + ")";              log.w(tag, database_create);             db.execsql(database_create);         }          @override         public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {             log.w(tag, "upgrading database version " + oldversion + " "                     + newversion + ", destroy old data");             db.execsql("drop table if exists " + sqlite_table);             oncreate(db);         }  /*    public dbhandler(context ctx) {         this.mctx = ctx;     }*/ /*     public dbhandler open() throws sqlexception {         mdbhelper = new databasehelper(mctx);         mdb = mdbhelper.getwritabledatabase();         return this;     }*/  /*    public void close() {         if (mdbhelper != null) {             mdbhelper.close();         }     }*/      public void createrecipe(string name,                               string type, string ingred) {          contentvalues initialvalues = new contentvalues();         //initialvalues.put(column_code, code);         initialvalues.put(column_name, name);         initialvalues.put(column_type, type);         initialvalues.put(column_ingred, ingred);          sqlitedatabase db = this.getwritabledatabase();         db.insert(sqlite_table, null, initialvalues);         db.close();     }      public boolean deleteallrecipes() {         int donedelete = 0;          sqlitedatabase mdb = this.getwritabledatabase();         donedelete = mdb.delete(sqlite_table, null , null);         log.w(tag, integer.tostring(donedelete));         return donedelete > 0;      }      public cursor fetchrecipesbyname(string inputtext) throws sqlexception {         sqlitedatabase mdb = this.getwritabledatabase();         log.w(tag, inputtext);         cursor mcursor = null;         if (inputtext == null  ||  inputtext.length () == 0)  {             mcursor = mdb.query(sqlite_table, new string[] {column_rowid,                             column_name, column_type, column_ingred},                     null, null, null, null, null);          }         else {              mcursor = mdb.query(true, sqlite_table, new string[] {column_rowid,                             column_name, column_type, column_ingred},                     column_name + " '%" + inputtext + "%'" + " or " +                             column_type + " '%" + inputtext + "%'" + " or " +                             column_ingred + " '%" + inputtext + "%'",                     null, null, null, null, null);         }         if (mcursor != null) {             mcursor.movetofirst();         }         return mcursor;      }       public cursor fetchallrecipes() {         sqlitedatabase mdb = this.getwritabledatabase();          cursor mcursor = mdb.query(sqlite_table, new string[] {column_rowid,                         column_name, column_type, column_ingred},                 null, null, null, null, null);          if (mcursor != null) {             mcursor.movetofirst();         }         return mcursor;     }      public void insertsomerecipes() {          createrecipe("blackened salmon","dinner","salmon");     }      public boolean deleterecipe(string name){         sqlitedatabase mdb = this.getwritabledatabase();         boolean result = false;          string sql_query = "select * " + sqlite_table + " " + column_name + " = \"" + name + "\"";          cursor mycursor = mdb.rawquery(sql_query, null);         recipes myrecipe = new recipes();          if (mycursor.movetofirst()){             myrecipe.setid(mycursor.getint(0));             mdb.delete(sqlite_table, column_rowid + " = ?", new string[]{string.valueof(myrecipe.getid())});             mycursor.close();             result = true;         }          mdb.close();         return result;      } 

}

the simplest technique can follow store image path in database table string , can whenever need image , decode using bitmapfactory.decodefile. storing base64string fine string have large so, if see path of image comparatively shorter string

for example:

  • define parameter in createrecipe() method named string imagepath like:

public void createrecipe(string name,string type, string ingred,string imgpath){}

  • when make call function pass image path ever have like:

createrecipe(name,type,ingred,path_of_image)

to imagepath can that:

string destination = new file(environment.getexternalstoragedirectory(), name_of_your_image+ ".jpg"); fileinputstream in = new fileinputstream(destination); string photopath = destination.getabsolutepath(); 
  • finally when call fetchrecipesbyname() or fetchallrecipes() method cursor have path stored against each entry can path string , pass below function bitmap in return.
private bitmap getbitmapfrompath(string path) {          bitmapfactory.options btmapoptions = new bitmapfactory.options();      btmapoptions.insamplesize = 2;      bitmap bm = bitmapfactory.decodefile(path, btmapoptions);      return bm; } 

note: should take care if storing path of image , deleted gallery in return cannot have bitmap against path should manage if image dosen't exist use alternative image.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -