package com.example.downloadimagefile;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView imageview;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
imageview = (ImageView)findViewById(R.id.imageView);
button = (Button)findViewById(R.id.button);
}
public void webgetImage(View v)
{
Bitmap imgbitmap = getImageUrl();
//이미지가 존재한다면
if(imgbitmap !=null)
{
//이미지뷰에 웹에서 불러온 이미지 부착
imageview.setImageBitmap(imgbitmap);
}
else{
Toast.makeText(this, "value is 0", 0).show();
}
}
//웹에서 이미지를 불러오는 메소드
private Bitmap getImageUrl()
{
Bitmap bitmap =null;
try{
//웹 사이트에 접근할수 있도록 도와주는 클래스 선언
URL url = new URL(uri);
//웹 사이트 접속
HttpURLConnection comm = (HttpURLConnection)url.openConnection();
//연결설정
comm.connect();
//데이터를 가져옴
int imageSize = comm.getContentLength();
//스트림에 넣어줌
BufferedInputStream bis = new BufferedInputStream(comm.getInputStream() ,imageSize);
//비트맵에 넣어줌
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
}catch(Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
return bitmap;
}
}
코드는 틀린부분이 없는것같은데
이미지가 없다고 토스트만 계속 뜨네요 ..ㅠㅠ 도와주세요