gpt4 book ai didi

android - 如何将图像存储为 Sqlite 中的 blob 以及如何检索它?

转载 作者:IT老高 更新时间:2023-10-28 13:13:38 25 4
gpt4 key购买 nike

我想将图像(来自 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]

最佳答案

这是我用于我的应用程序的代码

此代码将从 url 获取图像并将其转换为字节数组

byte[] logoImage = getLogoImage(IMAGEURL);

private byte[] getLogoImage(String url){
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();

InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}

return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}

为了将图像保存到数据库,我使用了此代码。

 public void insertUser(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
String delSql = "DELETE FROM ACCOUNTS";
SQLiteStatement delStmt = db.compileStatement(delSql);
delStmt.execute();

String sql = "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)";
SQLiteStatement insertStmt = db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, Integer.toString(this.accId));
insertStmt.bindString(2,this.accName);
insertStmt.bindBlob(3, this.accImage);
insertStmt.executeInsert();
db.close();
}

要检索图像,这是我使用的代码。

public Account getCurrentAccount() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "SELECT * FROM ACCOUNTS";
Cursor cursor = db.rawQuery(sql, new String[] {});

if(cursor.moveToFirst()){
this.accId = cursor.getInt(0);
this.accName = cursor.getString(1);
this.accImage = cursor.getBlob(2);
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
if(cursor.getCount() == 0){
return null;
} else {
return this;
}
}

最后把这张图片加载到一个imageview中

logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage, 
0,currentAccount.accImage.length));

关于android - 如何将图像存储为 Sqlite 中的 blob 以及如何检索它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7331310/

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