2012-11-21 63 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(); 
相关问题