Android > HttpURLConnection - ¼¹öÀ̹ÌÁö ºÒ·¯¿À±â ¿¹Á¦
µî·ÏÀÏ : 2017-07-06 17:35
Á¶È¸¼ö : 52,990
¼¹ö¿¡ ÀÖ´Â À̹ÌÁö¸¦ ImageView ¿¡ »Ñ¸®´Â ¿¹Á¦ ÀÔ´Ï´Ù.
public class LoadImageServer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String imagePath = "http://entersoft.kr/images/1.png" ;
ImageView imgView = (ImageView) findViewById(R.id.imgView) ;
Bitmap bm = LoadImage( imagePath ) ; // ÀÌ·¸°Ô imagePath¸¸ ³Ö¾îÁÖ¸é, BitmapÀ¸·Î ¹ÝȯÀÌ µÈ´Ù.
imgView.setImageBitmap( bm ) ; // ±× BitmapÀ» Âï±â¸¸ ÇÏ¸é µÈ´Ù.
}
private Bitmap LoadImage( String imagePath ) {
InputStream inputStream = OpenHttpConnection( imagePath ) ;
Bitmap bm = BitmapFactory.decodeStream( inputStream ) ;
return bm;
}
private InputStream OpenHttpConnection(String imagePath) {
InputStream stream = null ;
try {
URL url = new URL( imagePath ) ;
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection() ;
urlConnection.setRequestMethod("GET");
urlConnection.connect() ;
if( urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK ) {
stream = urlConnection.getInputStream() ;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stream ;
}
}