서버에서 이미지 불러오기

2011. 1. 13. 09:34Scrapbook/개발 및 프로그래밍

반응형
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;
//    }

반응형