I have a Sqlite database,where I am inserting data directly which should appear as prestored data for my app user.I found a way by converting image to bitmap.But how to convert more than one image using this code and how can I insert that bitmap using my DataBaseHandler class
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] img=bos.toByteArray();
DataBaseHandler.java
public class DataBaseHandler extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "products.db";
public static final String CONTACTS_TABLE_NAME = "product";
public static final String CONTACTS_COLUMN_ID = "pid";
public static final String CONTACTS_COLUMN_NAME = "pname";
public static final String CONTACTS_COLUMN_EMAIL = "pspec";
public static final String CONTACTS_COLUMN_STREET = "pprice";
public static final String CONTACTS_COLUMN_CITY = "pfeature";
public static final String CONTACTS_COLUMN_PHONE = "pimage";
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS product(pimage BLOB,pid INTEGER PRIMARY KEY,pname TEXT,pprice NUMERIC,pspec TEXT,pfeature TEXT)");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 3',4000,'Solar garden / pathway light,Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA ,Material:Stainless steel ,WaterProof and safe ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 4',4500,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
can I store images to database as I did for other values using below query.
db.execSQL("INSERT INTO TABLE NAME(img) VALUES('R.Drawable.img')");
Aucun commentaire:
Enregistrer un commentaire