gpt4 book ai didi

android - 将图像保存在数据库中 erro & decodeByteArray give null

转载 作者:行者123 更新时间:2023-11-30 01:55:05 26 4
gpt4 key购买 nike

我正在尝试下载图像并将其保存到数据库我下载它并将我保存到 BLOB 字段当我想检索它时它得到空值。我搜索了很多并检查了很多东西但我不知道在哪里问题:(。

保存图片部分:

MyDataBase = new MyDatabase(this);
mydb = MyDataBase.getWritableDatabase();

byte[] tempval;
for (int j = 0; j < myarray.arrtitle.length; j++)
{
tempval = saveimage(myarray.arrpic.get(j),myarray.arrpath[j]);
mydb.execSQL("INSERT INTO news (title,content,image) VALUES (\"" + myarray.arrtitle[j] +
"\",\"" + myarray.arrcontent[j] + "\",\"" + tempval + "\")");
}
mydb.close()

保存图像()函数:

try {
////////////////get bitmap to byte
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] image = bos.toByteArray();

return image;

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

从数据库读取:

          try 
{
String temp = null;
Bitmap tempbit = null;
ArrayList<Bitmap> tempbit2 = new ArrayList<Bitmap>();
ArrayList<String> tempstring = new ArrayList<String>();

MyDataBase = new MyDatabase(this);
mydb = MyDataBase.getReadableDatabase();
Cursor cu = mydb.rawQuery("SELECT * FROM news",null);
cu.moveToFirst();
for (int i = 0;i<cu.getCount();i++)
{

temp = cu.getString(cu.getColumnIndex("title"));
byte[] imag = cu.getBlob(cu.getColumnIndex("image"));
tempbit = BitmapFactory.decodeByteArray(imag, 0, imag.length); // i get null here
tempbit2.add(tempbit);
tempstring.add(temp);
cu.moveToNext();
Toast.makeText(getApplicationContext(), "" + tempbit + i, Toast.LENGTH_LONG).show();

}
////////////////////////////show in list view
ListViewAdapte adapter = new ListViewAdapte(Description.this, R.layout.listview, tempstring , tempbit2);
MyList.setAdapter(adapter);
MyList.setOnItemClickListener(this);

} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "error on load from db", Toast.LENGTH_LONG).show();
}

当我 Toast tempbit 时它让我为空

最佳答案

我应该将图片保存为contentview

       ContentValues tempval = new ContentValues();
for (int j = 0; j < myarray.arrtitle.length; j++)
{
mydb.execSQL("INSERT INTO news (title,content) VALUES (\"" + myarray.arrtitle[j] +
"\",\"" + myarray.arrcontent[j] + "\"" + ")");
}



for (int j = 0; j < myarray.arrpic.size(); j++)
{

tempval = saveimage(myarray.arrpic.get(j),myarray.arrpath[j]);

update ="title ='" + myarray.arrtitle[j] + "'" ;

mydb.update("news", tempval,update, null);

}

使用 contentview 可以保存图片 :)

关于android - 将图像保存在数据库中 erro & decodeByteArray give null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372982/

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