如何将位图从 ImageView 保存到数据库 sqlite?
Posted
技术标签:
【中文标题】如何将位图从 ImageView 保存到数据库 sqlite?【英文标题】:How to save bitmap from ImageView to database sqlite? 【发布时间】:2015-03-26 20:09:14 【问题描述】:我正在尝试从数据库 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);
活动
//******//
//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 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);
【问题讨论】:
你可以从这里试试***.com/questions/11790104/… 【参考方案1】:转换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();
【讨论】:
***.com/questions/11790104/…有更多示例以上是关于如何将位图从 ImageView 保存到数据库 sqlite?的主要内容,如果未能解决你的问题,请参考以下文章
如何在不使用 ImageView 的情况下从 API 端点下载图像或位图?
Android:如何在不加载完整位图的情况下将流式图像即时渲染到 ImageView?
Android - 在显示进度时将图像从 URL 加载到 ImageView(不保存图像)
SeekBar:从 Drawable 获取空位图,直到将其设置为 ImageView 或如何从 9patch 获取结果位图