2013-04-03 15 views
-2

我是新來的android和我創建了一個數據庫使用sqlite與images.When我查看我已插入數據庫中的所有數據只有熱(字符串行)顯示所有的記錄,但圖像只顯示第一行。任何人都可以幫助我,我現在絕望。如何循環查看來自sqlite數據庫的所有圖像

public String getHot() { 
      // TODO Auto-generated method stub 

    String[] columns = new String[] { KEY_HOTNESS }; 
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,null,null); 

    String hotness = ""; 
    int iRow = c.getColumnIndex(KEY_HOTNESS); 
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     hotness = hotness + c.getString(iRow) + "\n"; 
    } 
    return hotness; 
} 

public byte[] getImage1(int id) { 
    // TODO Auto-generated method stub 

    String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS, 
    KEY_IMAGE1, KEY_IMAGE2, KEY_IMAGE3, KEY_IMAGE4,KEY_IMAGE5 }; 

    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
      null, null); 
    if (c == null) { 
     return null; 
    } 
    byte[] iMage1 = null; 
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     iMage1 = c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE1)); 
    } 
    return iMage1; 
} 
+0

我已經嘗試添加「+‘\ n’」在圖像1行的前面,但它給了我一個error.So我如何讓它進入下一個記錄?日Thnx提前 –

回答

0

這是您的解決方案:但它也就會給你java.lang.OutOfMemoryError: bitmap如果你的圖像尺寸較大。

public ArrayList<Bitmap> getImage1(int id) { 
     // TODO Auto-generated method stub 
     ArrayList<Bitmap> a = new ArrayList<Bitmap>(); 
     Bitmap bmp; 

     String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS, 
       KEY_IMAGE1, KEY_IMAGE2, KEY_IMAGE3, KEY_IMAGE4, KEY_IMAGE5 }; 

     Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
       null, null); 
     if (c != null) { 
      do { 
       iMage1 = c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE1)); 
       bmp = BitmapFactory.decodeByteArray(iMage1, 0, 
         iMage1.length); 
       a.add(bmp); 
       iMage1 = c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE2)); 
       bmp = BitmapFactory.decodeByteArray(iMage1, 0, 
         iMage1.length); 
       a.add(bmp); 
       iMage1 = c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE3)); 
       bmp = BitmapFactory.decodeByteArray(iMage1, 0, 
         iMage1.length); 
       a.add(bmp); 
       iMage1 = c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE4)); 
       bmp = BitmapFactory.decodeByteArray(iMage1, 0, 
         iMage1.length); 
       a.add(bmp); 
       iMage1 = c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE5)); 
       bmp = BitmapFactory.decodeByteArray(iMage1, 0, 
         iMage1.length); 
       a.add(bmp); 

      } while (c.moveToNext()); 
     } 

     return a; 
    } 
相關問題