Android 将图像保存在db erro和decodeByteArray中为空
我正在尝试下载图像并将其保存到db。我下载图像并将其保存到BLOB字段,当我要检索它时,它会获取空值。我搜索了很多内容并检查了很多内容,但我不知道问题出在哪里: 保存图像部分:Android 将图像保存在db erro和decodeByteArray中为空,android,sqlite,save-image,Android,Sqlite,Save Image,我正在尝试下载图像并将其保存到db。我下载图像并将其保存到BLOB字段,当我要检索它时,它会获取空值。我搜索了很多内容并检查了很多内容,但我不知道问题出在哪里: 保存图像部分: MyDataBase = new MyDatabase(this); mydb = MyDataBase.getWritableDatabase(); byte[] tempval; for (int j = 0; j < myarray.arrtitle.length; j++) { tempval = s
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
{
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();
}
当我使用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可以保存图像:您正在使用Toast显示错误。将e.printStackTrace放在catch子句中,并让我们知道logcat中的异常。您可能遇到了OutOfMemory错误。@Glenn-我的应用程序中没有任何错误。我可以向你展示我的数据库,里面有很多数据。你说的OutOfMemory是什么意思?我以为你在保存图像时出错了?在saveImage函数中。检查是否出现异常/错误。您正在创建另一个位图作为字节[],它在内存中占用更多空间。
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);
}