2014년 8월 6일 수요일

[Android] URL주소에서 이미지 가져와서 보여주기

package com.example.test

import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class s_imgViewMain extends Activity {
 
 ImageView imgView;
 ViewFlipper flipper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imgView = (ImageView)findViewById(R.id.imgView);
        flipper = (ViewFlipper)findViewById(R.id.flipper);
        
        try {
         URL url = new URL("이미지 주소");
            URLConnection conn = url.openConnection();
            conn.connect();
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            imgView.setImageBitmap(bm);
  } catch (Exception e) {
  }
        
    }
}


주의사항!!
퍼미션 추가
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

댓글 없음:

댓글 쓰기