2016-07-27 48 views
1

我創建了一個簡單的應用程序,其中圖像從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它也顯示相同的錯誤

回答

2

Android的sqlite CursorWindow有一個固定大小的緩衝區,大多數配置是2MB。你不能在任何比這更大的行上移動。

不要在Android sqlite中存儲大的二進制數據,如圖像。改爲使用外部存儲並將路徑保存到數據庫中。

+0

如何使用外部存儲以及如何保存在DATABSE –

+0

確定的路徑,但我使用imagee.setImageResource(R.drawable.download);然後存儲ImageView中2或3 kb的圖像,然後它也顯示相同的錯誤原因 –

+0

可能您已經將大量數據存儲在數據庫中,並且查詢部分在光標窗口大小限制方面存在問題。 – laalto

0

檢查這個代碼有關如何將圖像中的android節省:

private String saveToInternalStorage(Bitmap bitmapImage){ 
     ContextWrapper cw = new ContextWrapper(getApplicationContext()); 
     // path to /data/data/yourapp/app_data/imageDir 
     File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
     // Create imageDir 
     File mypath=new File(directory,"profile.jpg"); 

     FileOutputStream fos = null; 
     try {   
      fos = new FileOutputStream(mypath); 
     // Use the compress method on the BitMap object to write image to the OutputStream 
      bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     } catch (Exception e) { 
       e.printStackTrace(); 
     } finally { 
       fos.close(); 
     } 
     return directory.getAbsolutePath(); 
    } 

你只需要你的形象的路徑添加到您的數據庫,而不是團塊圖像。

參考:Saving and Reading Bitmaps/Images from Internal memory in Android

相關問題