2012-11-21 62 views
1

我要實現與圖像的搜索列表視圖..我實現搜索方法,並列出詳細..我如何可以把圖像在角落裏的ListView ..數據庫存儲的圖像列表視圖中Android上

  • 圖像將在sqlite數據庫中...我如何從數據庫中獲取圖像?我可以如何將它放入陣列適配器?現在只有一個串包括

代碼是:

ArrayAdapter<String> adapter; 
private ArrayList<String> results1 = new ArrayList<String>(); 

here結果字符串將在適配器..我怎麼可以申報這些adapater圖像... 1

+0

那麼,你有什麼嘗試,什麼都沒有爲你工作? – Egor

+0

使用blob來存儲圖像,或存儲一個純文件,並將文件路徑作爲一個字符串在數據庫中。如果您使用數據庫,請使用遊標適配器而不是陣列適配器。 – njzk2

回答

0

你可以使用blob(byte [])將圖像存儲到sqlite數據庫中。

0

你可以通過轉換成base64字符串來存儲圖像。

這是解碼的base64字符串到圖像(位圖)的功能...

public static Bitmap decodeBase64(String input) 
    { 
     byte[] decodedByte = Base64.decode(input, 0); 
     return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
    } 

,這是將圖像爲Base64字符串

public static String encodeTobase64(Bitmap image) 
{ 
    Bitmap immagex=image; 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray(); 
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT); 

    return imageEncoded; 
} 
0

此答案讀取編碼功能從數據庫中的圖像,相應地更改爲您的要求:

SQLiteDatabase myDb;  
      String MySQL; 
      byte[] byteImage1 = null; 
      byte[] byteImage2 = null; 
      MySQL="create table emp1(_id INTEGER primary key autoincrement, sample TEXT not null, picture BLOB);"; 
      myDb = openOrCreateDatabase("Blob List", Context.MODE_PRIVATE, null); 
      myDb.execSQL(MySQL); 
      String s=myDb.getPath(); 
      textView.append("\r\n" + s+"\r\n");  
      myDb.execSQL("delete from emp1"); 
      ContentValues newValues = new ContentValues(); 
      newValues.put("sample", "HI Hello"); 


     try 
     { 
     FileInputStream instream = new FileInputStream("/sdcard/Pictures/picture.jpg"); 
     BufferedInputStream bif = new BufferedInputStream(instream); 
     byteImage1 = new byte[bif.available()]; 
     bif.read(byteImage1); 
     textView.append("\r\n" + byteImage1.length+"\r\n"); 
     newValues.put("picture", byteImage1); 

     long ret = myDb.insert("emp1", null, newValues); 
     if(ret<0) textView.append("\r\n!!! Error add blob filed!!!\r\n"); 
     } catch (IOException e) 
     { 
      textView.append("\r\n!!! Error: " + e+"!!!\r\n"); 
     } 


     Cursor cur = myDb.query("emp1",null, null, null, null, null, null); 
     cur.moveToFirst(); 
     while (cur.isAfterLast() == false) 
     { 
      textView.append("\r\n" + cur.getString(1)+"\r\n"); 
      cur.moveToNext(); 
     } 
    ///////Read data from blob field//////////////////// 
     cur.moveToFirst(); 
     byteImage2=cur.getBlob(cur.getColumnIndex("picture")); 
     bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length)); 
     textView.append("\r\n" + byteImage2.length+"\r\n"); 

     cur.close(); 

     myDb.close(); 
相關問題