byte[] imgData = new byte[fs.Length]
fs.Read(imgData, 0, fs.Length)
SqlConnection conn = new SqlConnection("...")
SqlDataAdapter da = new SqlDataAdapter("select ... ", conn)
DataSet ds = new DataSet()
da.Fill(ds)
SqlCommandBuilder cb = new SqlCommandBuilder(da)
ds.Tables[0].Rows[0]["img"] = imgData
da.Update(ds)
其中,img字段为Image类型
//获取数据库里的图片代码(省略)object ob = dataTable.Rows[0]["图片"]
byte[] buff = ob as byte[]
if (ob == null)
{
//报错
return
}
MemoryStream ms = new MemoryStream(buff, 0, buff.Length, true)
Bitmap bt = Bitmap.FromStream(ms) as Bitmap
if (bt == null)
{
//数据格式错误,请检查是否是图片
return
}
ms.Flush()
ms.Close()
//把bt显示出来,比如 pictureBox.Image = bt
如何将图片存入sqlite数据库1.将图片转化为byte数组
2.将byte数组放入contentvalues
3.执行数据库的insert *** 作,将contentvalues里面的值存入sqlite
代码大致如下://转换
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon)
0202020202020202 ByteArrayOutputStream baos = new ByteArrayOutputStream()02020202
0202020202020202 bm.compress(Bitmap.CompressFormat.PNG, 100, baos)02020202
0202020202020202 byte[] result = baos.toByteArray()0202//装入
ContentValues values = new ContentValues()
values.put("image", result)//插入数据库
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)