2009年9月13日日曜日

Android URL上の画像を表示する

URL(http://~)から動的に読み込む場合

1. (BufferedInputStream,ByteArrayOutputStream,BufferedOutputStreamなどで)読み込んだbyte配列をBitmapFactory.decodeByteArray()にセットしてBitmapを得、ImageView.setImageBitmap()にセットすれば表示できる

2. InputStream in = httpResponse.getEntity().getContent() とし、 BitmapFactory.decodeStream(in) にセットすれば表示できる

  1. httpResponse = dhc.execute(new HttpGet(imageURL));  
  2.   
  3. if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  4.   httpEntity = httpResponse.getEntity();  
  5.   final InputStream in = httpEntity.getContent();  
  6.   
  7.   Bitmap bitm = BitmapFactory.decodeStream(in);  
  8.   imageView.setImageBitmap(bitm);  
  9.   imageView.invalidate();           
  10. }  

0 件のコメント:

コメントを投稿