如何在 Sqlite & 中将图像存储为 blob如何找回它?
我想将图像(来自 url)存储到 sqlite 数据库中。
为此,我使用:
db = new DataBase(getApplicationContext());
URL url = new URL("http://sree.cc/wp-content/uploads/schogini_team.png");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer barb= new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1) {
barb.append((byte) current);
}
ContentValues filedata= new ContentValues();
filedata.put(DataBase.IMG_SRC,barb.toByteArray());
db.insert(DataBase.Table_Img, null, filedata);
在 Insert()
中:
public void insert(String tableImg, Object object,
ContentValues dataToInsert) {
// TODO Auto-generated method stub
String sql = "INSERT INTO "+tableImg+" ("+ID+","+IMG_SRC+") " +
"VALUES ('"+1+"','"+dataToInsert+"')";
db.execSQL(sql);
}
用于检索图像:
Cursor cursor = db.selectDataToShow(DataBase.Table_Img, DataBase.IMG_SRC);
byte[] imageByteArray=cursor.getBlob(cursor.getColumnIndex(DataBase.IMG_SRC));
cursor.close();
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
System.out.println(">>>>>>>>>>>>>>>>>>>>>> "+theImage);
所以这里我得到 null
。
在我的数据库中,图像的值存储为:Image=[B@43e5ac48]
I want to store an image(from url) into a sqlite database.
For that I use:
db = new DataBase(getApplicationContext());
URL url = new URL("http://sree.cc/wp-content/uploads/schogini_team.png");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer barb= new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1) {
barb.append((byte) current);
}
ContentValues filedata= new ContentValues();
filedata.put(DataBase.IMG_SRC,barb.toByteArray());
db.insert(DataBase.Table_Img, null, filedata);
In the Insert()
:
public void insert(String tableImg, Object object,
ContentValues dataToInsert) {
// TODO Auto-generated method stub
String sql = "INSERT INTO "+tableImg+" ("+ID+","+IMG_SRC+") " +
"VALUES ('"+1+"','"+dataToInsert+"')";
db.execSQL(sql);
}
For the retrieval of image:
Cursor cursor = db.selectDataToShow(DataBase.Table_Img, DataBase.IMG_SRC);
byte[] imageByteArray=cursor.getBlob(cursor.getColumnIndex(DataBase.IMG_SRC));
cursor.close();
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
System.out.println(">>>>>>>>>>>>>>>>>>>>>> "+theImage);
So here I got null
.
And in my database the value of image stored as: Image=[B@43e5ac48]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我用于我的应用程序的代码
此代码将从 url 获取图像并将其转换为字节数组
要将图像保存到数据库我使用了此代码。
为了检索图像,这是我使用的代码。
最后将此图像加载到图像视图中
Here the code i used for my app
This code will take a image from url and convert is to a byte array
To save the image to db i used this code.
To retrieve the image back this is code i used.
Finally to load this image to a imageview
在 DBAdaper 即数据库帮助程序类中声明这样的表
插入这样的值,
图像转换为 byte[]
首先在 DEAdaper 类中将
检索图像如下
将 byte[] 转换为图像
我认为此内容可以解决您的问题
in the DBAdaper i.e Data Base helper class declare the table like this
insert the values like this,
first convert the images as byte[]
in DEAdaper class
retrieve the image as follows
convert the byte[] into image
I think this content may solve your problem
在 insert()
希望它能帮助你。
In insert()
Hope it helps you.
对于一个ionic项目
现在我们将imgBBDD放入SqlLite
A服务器端(php)
for a ionic project
And now we put imgBBDD into SqlLite
A server side (php)
您可能还想对 Base64 进行编码和解码
you may also want to encode and decode to/from base64