2012-11-27 102 views
0

我在Google數據庫上搜索了一些教程。我在將圖像插入數據庫時​​遇到了一些問題。我曾嘗試過很多方法(如setImageBitmap,setImageDrawable,setImageResource),但他們都不工作。我已經將一些圖像插入我的drawable-hdpi插入圖像到SQL

下面是我的一些代碼插入:

public void insertIntoTable(){ 
    try{ 
     mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 

     mydb.execSQL("INSERT INTO " + TABLE + "(DNAME, DTAG, DSUMMARY, DINGRE, DSTEP) VALUES('Chicken Cabonara','Chicken','aaa', '3', '5')"); 
     mydb.execSQL("INSERT INTO " + TABLE + "(DNAME, DTAG, DSUMMARY, DINGRE, DSTEP) VALUES('Seafood Spagetti','Fish','bbb', '5', '3')"); 
     mydb.execSQL("INSERT INTO " + TABLE + "(DNAME, DTAG, DSUMMARY, DINGRE, DSTEP) VALUES('Tomyam Soup','Soup','ccc', '3', '1')"); 

     mydb.close(); 
    } 
} 

我用於顯示數據代碼:

public void showTableValues(){ 
    try{ 
     mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
     Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE, null); 
     System.out.println("COUNT : " + allrows.getCount()); 
     Integer cindex = allrows.getColumnIndex("DNAME"); 
     Integer cindex1 = allrows.getColumnIndex("DTAG"); 
     Integer cindex2 = allrows.getColumnIndex("DSUMMARY"); 
     Integer cindex3 = allrows.getColumnIndex("DINGRE"); 
     Integer cindex4 = allrows.getColumnIndex("DSTEP"); 

     TextView t = new TextView(this); 
     t.setText("========================================"); 
     Linear.addView(t); 

     if(allrows.moveToFirst()){ 
      do{ 
       LinearLayout did_row = new LinearLayout(this); 
       LinearLayout dname_row = new LinearLayout(this); 
       LinearLayout dtag_row= new LinearLayout(this);   
       LinearLayout dsummary_row= new LinearLayout(this); 
       LinearLayout dingre_row= new LinearLayout(this); 
       LinearLayout dstep_row= new LinearLayout(this); 

       final TextView did_ = new TextView(this); 
       final TextView dname_ = new TextView(this); 
       final TextView dtag_ = new TextView(this); 
       final TextView dsummary_ = new TextView(this); 
       final TextView dingre_ = new TextView(this); 
       final TextView dstep_ = new TextView(this); 
       final TextView sep = new TextView(this); 

       String DID = allrows.getString(0); 
       String DNAME= allrows.getString(1); 
       String DTAG= allrows.getString(2); 
       String DSUMMARY= allrows.getString(3); 
       String DINGRE= allrows.getString(4); 
       String DSTEP= allrows.getString(5); 

       System.out.println("DNAME " + allrows.getString(cindex) + " DTAG : "+ allrows.getString(cindex1) + " DSUMMARY : "+ allrows.getString(cindex2) + " DINGRE : "+ allrows.getString(cindex3) + " DSTEP : "+ allrows.getString(cindex4)); 
       System.out.println("DID : "+ DID + " || DNAME " + DNAME + "|| DTAG : "+ DTAG + "|| DSUMMARY : "+ DSUMMARY + "|| DINGRE : "+ DINGRE + "|| DSTEP : "+ DSTEP); 

       did_.setText("ID : " + DID); 
       did_row.addView(did_); 
       Linear.addView(did_row); 

       dname_.setText("NAME : "+ DNAME); 
       dname_row.addView(dname_); 
       Linear.addView(dname_row); 

       dtag_.setText("TAG : " + DTAG); 
       dtag_row.addView(dtag_); 
       Linear.addView(dtag_row); 

       dsummary_.setText("SUMMARY : "+ DSUMMARY); 
       dsummary_row.addView(dsummary_); 
       Linear.addView(dsummary_row); 

       dingre_.setText("INGREDIENT : " + DINGRE); 
       dingre_row.addView(dingre_); 
       Linear.addView(dingre_row); 

       dstep_.setText("STEP : "+ DSTEP); 
       dstep_row.addView(dstep_); 
       Linear.addView(dstep_row); 

       sep.setText("---------------------------------------------------------------"); 
       Linear.addView(sep); 
      } 
      while(allrows.moveToNext()); 
     } 
     mydb.close(); 
    } 
} 

有人能告訴我插入圖像(PNG)的方式進入數據庫,並顯示圖片?謝謝。

+2

您可以保存圖像路徑數據庫並保存圖像文件夾中。這是最好的方式 –

+0

已被問及在這裏回答例如http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it - 但只存儲圖像的路徑通常是一個更好的主意。存儲小圖標將是好的 – zapl

+0

@NiravRanpara所以,如果圖像是在這個路徑,/SqlRecipe/res/drawable-hdpi/img1.png,我必須保存在我插入爲文本? –

回答

0

您必須將圖像作爲blob數據插入。您可以查看this鏈接。

您也可以參考Link1