2011-12-06 13 views
0

私は画像が表示されるべきgridviewを持っています。私はデータベース内のすべての画像をブロブとして保存しました。 Iamはhashmapを使用し、それをarraylistに追加します。私はまた、それぞれのイメージと一緒にタイトルを持っています。次のように私のコードです:データベースから画像を取得するandroid

ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); 
     Cursor cr = dbAdapter.fetchAllMenuData(); 


      HashMap<String, Object> map ; 


      cr.moveToFirst(); 

       int k=0; 
       while(!cr.isAfterLast()) 
       { 
       map= new HashMap<String,Object>(); 
       map.put("Image", cr.getBlob(cr.getColumnIndex("Image"))); 
       map.put("Title", cr.getString(cr.getColumnIndex("Title"))); 
       k++; 
       mylist.add(map); 
       map=null; 
       cr.moveToNext(); 
       } 

     MySimpleAdapter adapter = new MySimpleAdapter(Menu.this, mylist, 
       R.layout.menugrid, new String[] { "Title", "Image" }, 
       new int[] { R.id.item_title, R.id.img }); 

     list.setAdapter(adapter); 

を今、イメージbyte[]の形態です。

私はViewHolderを使用して、特定の画像とタイトルをgridviewitemに設定しています。

holder.textView1.setText(mData.get(position).get("Title") 
        .toString()); 
      // holder.textView2.setText(mData.get(position).get("Description").toString()); 

      byte[] blob= toByteArray(mData.get(position).get("Image")); 
     Bitmap bt=BitmapFactory.decodeByteArray(blob,0,blob.length); 
      holder.imageView1.setImageBitmap(bt); 

を次のようにコードがあるという問題がhashmapHashMap<String, Object>ようので、私は、書き込みにバイト配列にオブジェクトを変換する方法を持っていたです。方法は以下の通りです:

public byte[] toBitmap (Object obj) 
    { 
     byte[] bytes = null; 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     try { 
     ObjectOutputStream oos = new ObjectOutputStream(bos); 
     oos.writeObject(obj); 
     oos.flush(); 
     oos.close(); 
     bos.close(); 
     bytes = bos.toByteArray(); 
     return bytes; 

     } 
     catch (IOException ex) { 
     return null; //TODO: Handle the exception 
     } 

このメソッドは正しくbyte[]を返しています。しかし、ビットマップに変換することができます BitmapFactory.decodeByteArray(blob,0,blob.length);nullを返します。したがってimageviewに設定できませんでした。

答えて

2

これを試してください。これはあなたを助けるかもしれません。

byte[] pic=(cursor.getBlob(position)); 
    ByteArrayInputStream imageStream = new ByteArrayInputStream(pic); 
    Bitmap theImage= BitmapFactory.decodeStream(imageStream); 
+0

:こんにちは、これを手伝ってもらえますか? http://stackoverflow.com/questions/15954896/how-to-save-image-taken-from-camera-and-show-it-to-listview-crashes-with-ille .Thanks – George

関連する問題