반응형
안드로이드를 개발 할 때 OpenApi 등을 이용하여 이미지를 표시하고 합니다.
이런 경우 이미지 URL을 Bitmap으로 변경하여 ImageView에 표시하기 됩니다.
아래의 소스는 아미지 URL을 Bitmap으로 변경해주는 소스입니다.
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageHelper {
public static Bitmap getImage(String strUrl){
URL url = null;
HttpURLConnection conn = null;
InputStream is = null;
BufferedInputStream bis = null;
Bitmap bm = null;
try {
url = new URL(strUrl);
conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
} catch (Exception e) {
//throw e;
return null;
}finally{
try{
if(is != null) is.close();
if(bis != null) bis.close();
}catch(Exception e){}
}
return bm;
}
}
반응형
'안드로이드' 카테고리의 다른 글
안드로이드 - 리스트뷰에 버튼넣기 (2) | 2015.04.28 |
---|---|
안드로이드 - 도움말 파일(CHM) (0) | 2015.04.25 |
안드로이드 - 커스텀 리스트뷰(ListView) 만들기 (1) | 2015.04.24 |
안드로이드 - 버튼 이벤트 처리 (0) | 2015.04.24 |
애드몹(AdMob) There was a problem getting an ad response. ErrorCode: 1 (0) | 2015.04.24 |
댓글