我創建了一個簡單的應用程序,其中圖像從ImageView存儲到數據庫。 但是,當點擊回顧按鈕顯示索引1請求大小爲3 我不知道什麼事情出錯了。 數據庫類:索引1請求大小爲3
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_IMAGE_TABLE = "CREATE TABLE " +TABLE_NAME + "("
+ IMAGE_KEY + " BLOB)";
db.execSQL(CREATE_IMAGE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(byte[]image)throws SQLiteException
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(IMAGE_KEY,image);
long result= db.insert(TABLE_NAME,null,cv);
if(result==-1)
return false;
else
return true;
}
public Cursor getAllData()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
byte[]img=res.getBlob(0);
return res;
}
這是活動類:
public void button2(View view)
{
try {
Cursor res = myDb.getAllData();
if (res ==null) {
showMessage("error", "no data found");
} else {
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("id:" + res.getBlob(0) + "\n");
byte[] image = res.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
image.length);
imagee.setImageBitmap(bmp);
}
// showMessage("DATA", buffer.toString());
}
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(),
Toast.LENGTH_LONG).show();
}}
public void buttonn(View view)
{
Bitmap bitmap = ((BitmapDrawable) imagee.getDrawable()).getBitmap();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
byte[] data = outputStream.toByteArray();
boolean isInserted = myDb.insertData(data);
if (isInserted == true)
Toast.makeText(getBaseContext(), "Registration Succes!",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "No Record Registered!",
Toast.LENGTH_SHORT).show();
}
}
我想大多數,但無法從res.movetoNext沒有的事兒。變化,但表現出同樣的錯誤和使用res.movetoFirst它也顯示相同的錯誤
如何使用外部存儲以及如何保存在DATABSE –
確定的路徑,但我使用imagee.setImageResource(R.drawable.download);然後存儲ImageView中2或3 kb的圖像,然後它也顯示相同的錯誤原因 –
可能您已經將大量數據存儲在數據庫中,並且查詢部分在光標窗口大小限制方面存在問題。 – laalto