반응형
http://drkein.tistory.com/169
안드로이드에서 서버의 이미지 파일을 불러와 이미지 뷰에 붙이기 할 때..
안드로이드에서 서버의 이미지 파일을 불러와 이미지 뷰에 붙이기 할 때..
서버에서 이미지 불러서 비트맵 만드는 방법.
/*
* 느리긴 한데 안정적인 동작..
*/
// public static Bitmap getRemoteImage(URL url){
// System.out.println("IMAGE LOAD ----------------------------------> 1 " + url.toString());
// Bitmap bm = null;
// HttpGet httpRequest = null;
// try {
// httpRequest = new HttpGet(url.toURI());
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
// HttpClient httpclient = new DefaultHttpClient();
// try {
// HttpResponse response = (HttpResponse)httpclient.execute(httpRequest);
// HttpEntity entity = response.getEntity();
// BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
// InputStream instream = bufHttpEntity.getContent();
// bm = BitmapFactory.decodeStream(instream);
// System.out.println("IMAGE LOAD ----------------------------------> 2 ");
// return bm;
// } catch (ClientProtocolException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return null;
// }
// public Bitmap getImage(String address){
// URL url;
// InputStream is;
// try {
// url = new URL(address);
// is = (InputStream)url.getContent();
// } catch (Exception e) {
// e.printStackTrace();
// return null;
// }
//
// BitmapDrawable d = (BitmapDrawable)Drawable.createFromStream(is, "src");
// return d.getBitmap();
// }
/*
* 쓸만함.
*/
public static Bitmap getRemoteImage(URL url){
HttpURLConnection conn;
try {
conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
is.close();
return bm;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/*
* 비트맵 팩토리에서 디코딩 에러나는 확률이 높음.
*/
// public static Bitmap getRemoteImage(URL url){
// Bitmap bm = null;
// try{
// URLConnection conn = url.openConnection();
// conn.connect();
// BufferedInputStream bis = new BufferedInputStream(conn.getInputStream(), 10);
// bm = BitmapFactory.decodeStream(bis);
// bis.close();
// }catch(Exception e){}
// return bm;
// }
반응형
'Scrapbook > 개발 및 프로그래밍' 카테고리의 다른 글
안드로이드에서 Thread 돌리기 (0) | 2011.01.14 |
---|---|
네트워크에서 XML 파일 가져와 파싱하기 (0) | 2011.01.13 |
Spinner 사용하기 (0) | 2011.01.12 |
[Eclipse] Outline View 아이콘의 의미 (0) | 2011.01.07 |
안드로이드 inflate LayoutInflater 에 관한 고찰. (0) | 2011.01.06 |