沒什麼好解說的
1) 宣告
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = null;
try {
bitmap = DownloadImage(
"http://upload.wikimedia.org/wikipedia/commons/4/46/Flag_of_the_Republic_of_China.JPG");
} catch (IOException e) {
e.printStackTrace();
}
img = (ImageView) findViewById(R.id.image);
img.setImageBitmap(bitmap);
}
2) method 建立連線,並下載圖片
private Bitmap DownloadImage(String myurl) throws IOException
{
Bitmap bitmap = null;
InputStream in = null;
URL url = new URL(myurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
try {
in = new BufferedInputStream(con.getInputStream());
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
3) 執行結果
沒有留言:
張貼留言