gpt4 book ai didi

android - 如何将位图从 ImageView 保存到数据库 sqlite?

转载 作者:搜寻专家 更新时间:2023-11-01 07:55:35 24 4
gpt4 key购买 nike

我正在尝试从数据库 sqlite 上的 ImageView 保存 Bitmat,但我做不到。

我保存字符串,但我不知道图像。

我的代码

BBDD 类

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);

最佳答案

转换Bitmap-->字节数组,然后将字节数组插入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();
}

关于android - 如何将位图从 ImageView 保存到数据库 sqlite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28183558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com