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
を使用して、特定の画像とタイトルをgridview
のitem
に設定しています。
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);
を次のようにコードがあるという問題がhashmap
がHashMap<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
に設定できませんでした。
:こんにちは、これを手伝ってもらえますか? http://stackoverflow.com/questions/15954896/how-to-save-image-taken-from-camera-and-show-it-to-listview-crashes-with-ille .Thanks – George