2011-12-06 7 views
0

나는 이미지가 표시되어야하는 gridview이 있습니다. 데이터베이스의 모든 이미지를 얼룩으로 저장했습니다. Ion은 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을 사용하여 특정 이미지와 제목을 의 item으로 설정하고 있습니다. 문제는이 HashMap<String, Object> 그래서 기록에 바이트 배열 오브젝트로 변환하는 방법을 가지고 같은 hashmap가있다

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); 

를 다음과 같이 코드이다. 이 방법이 제대로 byte[]을 반환

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 
     } 

: 다음과 같은 방법이다. 하지만 비트 맵으로 변환 할 수 있습니다 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

관련 문제