如何应用ImageView读取网络图片资源

本教程介绍如何使用Android的ImageView从网络加载图片。主要步骤包括利用HttpURLConnection获取图片数据及使用BitmapFactory将数据转换成图像资源。示例代码展示了按钮点击事件触发图片下载的过程。

这个教程将演示如何应用ImageView读取网络图片资源。有两个比较重要的知识点需要特别注意:

  • 应用HttpURLConnection获得图片数据。
  • 通过BitmapFactory将数据转换为可供ImageView识别的图像资源。


Layout 源代码 (XML):

?[Copy to clipboard] View Code XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, HTTPImage load test"
    />
    <Button id="@+id/get_imagebt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get an image"
    android:layout_gravity="center"
    />  
    <ImageView id="@+id/imview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>

主程序源代码 (Java):

01. import java.io.IOException;
02. import java.io.InputStream;
03. import java.net.HttpURLConnection;
04. import java.net.MalformedURLException;
05. import java.net.URL;
06. import java.util.HashMap;
07. import java.util.Map;
08. import java.util.Random;
09.
10. import android.app.Activity;
11. import android.graphics.Bitmap;
12. import android.graphics.BitmapFactory;
13. import android.os.Bundle;
14. import android.util.Log;
15. import android.view.View;
16. import android.widget.Button;
17. import android.widget.EditText;
18. import android.widget.ImageView;
19.
20. public class HTTPTest extends Activity {
21.
22. ImageView imView;
23. String imageUrl="http://11.0.6.23/";
24. Random r;
25. /** Called when the activity is first created. */
26. @Override
27. public void onCreate(Bundle icicle) {
28. super.onCreate(icicle);
29. setContentView(R.layout.main);
30. r= new Random();
31.
32. Button bt3= (Button)findViewById(R.id.get_imagebt);
33. bt3.setOnClickListener(getImgListener);
34. imView = (ImageView)findViewById(R.id.imview);
35. }
36.
37. View.OnClickListener getImgListener = new View.OnClickListener()
38. {
39.
40. @Override
41. public void onClick(View view) {
42. // TODO Auto-generated method stub
43.
44. //i tried to randomize the file download, in my server i put 4 files with name like
45. //png0.png, png1.png, png2.png so different file is downloaded in button press
46. int i =r.nextInt()%4;
47. downloadFile(imageUrl+"png"+i+".png");
48. Log.i("im url",imageUrl+"png"+i+".png");
49. }
50.
51. };
52.
53. Bitmap bmImg;
54. void downloadFile(String fileUrl){
55. URL myFileUrl =null;
56. try {
57. myFileUrl= new URL(fileUrl);
58. } catch (MalformedURLException e) {
59. // TODO Auto-generated catch block
60. e.printStackTrace();
61. }
62. try {
63. HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
64. conn.setDoInput(true);
65. conn.connect();
66. int length = conn.getContentLength();
67.
68. InputStream is = conn.getInputStream();
69.
70. bmImg = BitmapFactory.decodeStream(is);
71. imView.setImageBitmap(bmImg);
72. } catch (IOException e) {
73. // TODO Auto-generated catch block
74. e.printStackTrace();
75. }
76. }
77. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值