Sunday, 15 February 2015

How to convert bitmap to byte array and put it to database table in Android? -



How to convert bitmap to byte array and put it to database table in Android? -

at time, have case in developing android application, right have class called dbhelper, class utilize execute operation create table , operation create trigerrs , on.. in case tried execute sql statement inserting info in table, sql operation not working, can help me? need help here.

here's class..

public class dbhelper extends sqliteopenhelper { private context mcontext; private static final string db_name ="schoolmap.db"; private static final int db_version=1; //constructor public dbhelper(context context) { super(context, db_name, null, db_version); this.mcontext = context; } private static final string db_table_foto = "create table " + "foto" + "(" + id + " integer primary key autoincrement, " + caption_foto + " varchar(20), " + image + " blob);"; @override public void oncreate(sqlitedatabase db) { db.execsql(db_table_foto); //in line tried add together image drawable folder bitmap b = bitmapfactory.decoderesource(mcontext.getresources(), r.drawable.a1); int bytes = b.getbytecount(); bytebuffer buffer = bytebuffer.allocate(bytes); //create new buffer b.copypixelstobuffer(buffer); byte[] array = buffer.array(); //the question right here, when seek code, it's not working, means: record not //inserted contentvalues values = new contentvalues(); values.put(caption_foto, "test"); values.put(image, array); // inserting row database.insert(foto, null, values); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(dbhelper.class.getname(),"upgrading database version " + oldversion + "to" + newversion + ",which destroy old data"); db.execsql("drop table if exists " + db_table_foto); oncreate(db); }

so mean, when running apps first time, tried add together image in application, sql operation not working, can help issue? help needed

in mutual case it's not thought set images database. can fast , easy access image resources without using db. reply here

the best way save image file scheme , save uri of string field in database. if need link record in database image in resources, can save resource id (like r.drawable.a1) in int field.

android android-sqlite

No comments:

Post a Comment