如何将位图从 ImageView 保存到数据库 sqlite?
How to save bitmap from ImageView to database sqlite?
我正在尝试从数据库 sqlite 上的 ImageView 保存 Bitmat,但我做不到。
我保存字符串,但我不知道图像。
我的代码
BBDDclass
public class BBDD extends SQLiteOpenHelper
{
String create = "CREATE TABLE info(usuario TEXT, " +
"ma TEXT, " +
"ti TEXT, " +
"loc TEXT, " +
"lati TEXT, " +
"longi TEXT, " +
"obs TEXT," +
"date TEXT, " +
"time TEXT, " +
"in TEXT, " +
"imgone BLOB, " +
"imgtwo BLOB, " +
"imgthree BLOB, " +
"imgfour BLOB) ";
public BBDD (Context contexto, String nombre, CursorFactory factory,
int version)
{
super(contexto, nombre, factory, version);
}
public void onCreate (SQLiteDatabase db)
{
db.execSQL(create);
}
public void onUpgrade (SQLiteDatabase db, int versionAnt, int versionNue)
{
db.execSQL(create);
}
}
activity
//******//
//declares
public BBDD info;
//******//
//onClick button to save
public void send (View view)
{
usuario = "ful";
ma = tv_ma.getText().toString();
ti = "T";
loc = tv_lug.getText().toString();
lati = tv_latib.getText().toString();
longi = tv_longb.getText().toString();
obs = txtStatus.getText().toString();
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
date = data;
SimpleDateFormat dfDate2 = new SimpleDateFormat("HH:mm:ss");
String data2="";
Calendar c2 = Calendar.getInstance();
data2=dfDate2.format(c2.getTime());
time = data2;
in = tv_infraccionb.getText().toString();
info = new BBDD(this, "BBDD", null, 1);
SQLiteDatabase db = info.getWritableDatabase();
//*****//
//validate empty variables
//*****//
if (db != null) {
ContentValues reg = new ContentValues();
reg.put("usuario", usuario);
reg.put("ma", ma);
reg.put("ti", ti);
reg.put("loc", loc);
reg.put("lati", lati);
reg.put("longi", longi);
reg.put("obs", obs);
reg.put("date", date);
reg.put("time", time);
reg.put("in", in);
我的问题是,我不知道如何在 "reg.put..."
上保存图片
if (db.insert("info", null, reg) != -1) {
Toast.makeText(this, "Reg insert", Toast.LENGTH_LONG)
.show();
db.close();
}
} else {
Toast.makeText(this, "Error", Toast.LENGTH_LONG)
.show();
}
}
}
我得到bundle Bitmap格式的图片
Bundle extras = getIntent().getExtras();
byte[] food = extras.getByteArray("picture");
Bitmap fo = BitmapFactory.decodeByteArray(food, 0, food.length);
img_ImageView.setImageBitmap(fo);
有什么建议吗?
更新
解决方案
Bitmap bitmap = ((BitmapDrawable)imageview.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap .compress(Bitmap.CompressFormat.PNG, 100, bos);
img = bos.toByteArray();
db=info.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("image", img);
db.insert("info",null, cv);
转换位图-->字节数组,然后将字节数组插入DB,
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.kitkat);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] img = bos.toByteArray();
ContentValues values = new ContentValues();
values.put("image", img);
SQLiteDatabase database = null;
try {
database = dbHelper.opendatabase();
database.insert("android_version", null, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
database.close();
}
我正在尝试从数据库 sqlite 上的 ImageView 保存 Bitmat,但我做不到。
我保存字符串,但我不知道图像。
我的代码
BBDDclass
public class BBDD extends SQLiteOpenHelper
{
String create = "CREATE TABLE info(usuario TEXT, " +
"ma TEXT, " +
"ti TEXT, " +
"loc TEXT, " +
"lati TEXT, " +
"longi TEXT, " +
"obs TEXT," +
"date TEXT, " +
"time TEXT, " +
"in TEXT, " +
"imgone BLOB, " +
"imgtwo BLOB, " +
"imgthree BLOB, " +
"imgfour BLOB) ";
public BBDD (Context contexto, String nombre, CursorFactory factory,
int version)
{
super(contexto, nombre, factory, version);
}
public void onCreate (SQLiteDatabase db)
{
db.execSQL(create);
}
public void onUpgrade (SQLiteDatabase db, int versionAnt, int versionNue)
{
db.execSQL(create);
}
}
activity
//******//
//declares
public BBDD info;
//******//
//onClick button to save
public void send (View view)
{
usuario = "ful";
ma = tv_ma.getText().toString();
ti = "T";
loc = tv_lug.getText().toString();
lati = tv_latib.getText().toString();
longi = tv_longb.getText().toString();
obs = txtStatus.getText().toString();
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
date = data;
SimpleDateFormat dfDate2 = new SimpleDateFormat("HH:mm:ss");
String data2="";
Calendar c2 = Calendar.getInstance();
data2=dfDate2.format(c2.getTime());
time = data2;
in = tv_infraccionb.getText().toString();
info = new BBDD(this, "BBDD", null, 1);
SQLiteDatabase db = info.getWritableDatabase();
//*****//
//validate empty variables
//*****//
if (db != null) {
ContentValues reg = new ContentValues();
reg.put("usuario", usuario);
reg.put("ma", ma);
reg.put("ti", ti);
reg.put("loc", loc);
reg.put("lati", lati);
reg.put("longi", longi);
reg.put("obs", obs);
reg.put("date", date);
reg.put("time", time);
reg.put("in", in);
我的问题是,我不知道如何在 "reg.put..."
上保存图片 if (db.insert("info", null, reg) != -1) {
Toast.makeText(this, "Reg insert", Toast.LENGTH_LONG)
.show();
db.close();
}
} else {
Toast.makeText(this, "Error", Toast.LENGTH_LONG)
.show();
}
}
}
我得到bundle Bitmap格式的图片
Bundle extras = getIntent().getExtras();
byte[] food = extras.getByteArray("picture");
Bitmap fo = BitmapFactory.decodeByteArray(food, 0, food.length);
img_ImageView.setImageBitmap(fo);
有什么建议吗?
更新
解决方案
Bitmap bitmap = ((BitmapDrawable)imageview.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap .compress(Bitmap.CompressFormat.PNG, 100, bos);
img = bos.toByteArray();
db=info.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("image", img);
db.insert("info",null, cv);
转换位图-->字节数组,然后将字节数组插入DB,
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.kitkat);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] img = bos.toByteArray();
ContentValues values = new ContentValues();
values.put("image", img);
SQLiteDatabase database = null;
try {
database = dbHelper.opendatabase();
database.insert("android_version", null, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
database.close();
}