為了連上網路, 務必加上權限
./AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
./src/com.tsots.ImageViewHTTP/Activity.java
//先找到一張網路圖片的網址
final String imageUrl = "http://www.sinaimg.cn/dongman/pictcol/2007-08-21/U2319P55T4D126138F50DT20070821161735_600small.jpg";
Bitmap mBitmap = null;
try
{
URL url = new URL(imageUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
mBitmap = BitmapFactory.decodeStream(is);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
TextView tv_title = (TextView) findViewById (R.id.tv_title);
ImageView iv = (ImageView) findViewById (R.id.iv_pic); tv_title.setText(imageUrl);
iv.setImageBitmap(mBitmap);
沒有留言:
張貼留言