一:查看网络图片

1、用tomcat跑起一个web项目,记下图片路径:

这里的ip切记不要写成localhost或者127.0.0.1 ,写成局域网的。

2、搭建android项目

涉及的文件如下图红框内

(1)、MainActivity

/netimage/src/net/thinkeye/image/ImageMainActivity.java

        
  1. packagenet.thinkeye.image;
  2. importnet.thinkeye.service.ImageService;
  3. importandroid.os.Bundle;
  4. importandroid.app.Activity;
  5. importandroid.graphics.Bitmap;
  6. importandroid.graphics.BitmapFactory;
  7. importandroid.view.View;
  8. importandroid.widget.Button;
  9. importandroid.widget.EditText;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.Toast;
  12. publicclassImageMainActivityextendsActivity{
  13. privateEditTextpathText;
  14. privateImageViewimageView;
  15. @Override
  16. protectedvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.image_main);
  19. pathText=(EditText)this.findViewById(R.id.imagepath);
  20. imageView=(ImageView)this.findViewById(R.id.imageView);
  21. Buttonb=(Button)this.findViewById(R.id.button);
  22. b.setOnClickListener(newButtonClickListener());
  23. }
  24. privatefinalclassButtonClickListenerimplementsView.OnClickListener{
  25. @Override
  26. publicvoidonClick(Viewv){
  27. Stringpath=pathText.getText().toString();
  28. try{
  29. byte[]data=ImageService.getImage(path);
  30. Bitmapbitmap=BitmapFactory.decodeByteArray(data,0,data.length);
  31. imageView.setImageBitmap(bitmap);//显示图片
  32. }catch(Exceptione){
  33. e.printStackTrace();
  34. Toast.makeText(getApplicationContext(),R.string.error,1).show();
  35. }
  36. }
  37. }
  38. }

2、

/netimage/src/net/thinkeye/service/ImageService.java

        
  1. packagenet.thinkeye.service;
  2. importjava.io.InputStream;
  3. importjava.net.HttpURLConnection;
  4. importjava.net.URL;
  5. importnet.thinkeye.utils.StreamTool;
  6. publicclassImageService{
  7. /**
  8. *获取网络图片的数据
  9. *@parampath
  10. *@return
  11. */
  12. publicstaticbyte[]getImage(Stringpath)throwsException{
  13. URLurl=newURL(path);
  14. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  15. conn.setConnectTimeout(5000);
  16. conn.setRequestMethod("GET");
  17. if(conn.getResponseCode()==200){
  18. InputStreamis=conn.getInputStream();
  19. returnStreamTool.read(is);
  20. }
  21. returnnull;
  22. }
  23. }

3、/netimage/src/net/thinkeye/utils/StreamTool.java

        
  1. packagenet.thinkeye.utils;
  2. importjava.io.ByteArrayOutputStream;
  3. importjava.io.InputStream;
  4. publicclassStreamTool{
  5. /**
  6. *读取流中的数据
  7. *@paramis
  8. *@return
  9. *@throwsException
  10. */
  11. publicstaticbyte[]read(InputStreamis)throwsException{
  12. ByteArrayOutputStreamo=newByteArrayOutputStream();
  13. byte[]buffer=newbyte[1024];
  14. intlen=0;
  15. while((len=is.read(buffer))!=-1){
  16. o.write(buffer,0,len);
  17. }
  18. is.close();
  19. returno.toByteArray();
  20. }
  21. }

4、页面布局部分:

image_main.xml

        
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/imagepath"/>
  10. <EditText
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="http://192.168.1.103:8888/SAMS/2.gif"
  14. android:id="@+id/imagepath"/>
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="@string/button"
  19. android:id="@+id/button"/>
  20. <ImageView
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:id="@+id/imageView"/>
  24. </LinearLayout>

5、定义字符常量

strings.xml

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">网络图片查看器</string>
  4. <stringname="imagepath">网络图片路径</string>
  5. <stringname="button">查看</string>
  6. <stringname="error">获取图片失败</string>
  7. </resources>

6、在AndroidManifest.xml文件里加入访问网络的权限,因为这里涉及到上网流量,会向用户收取一定的费用,在默认情况下是没有这个权限的,需要在此文件中申请权限,如下图:

好了,tomcat跑起来后在浏览器中能看到图片后,就把这个应用部署到模拟器上了。

部署完成后,就到了如下的页面:

点击上图的 查看 按钮,即可查看网络中的图片

二:查看页面源代码的功能和这个差不多,如下图:

下面显示源代码的部分的TextView需用ScrollView包裹起来,

        
  1. <ScrollView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content">
  4. <TextView
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:id="@+id/codeView"/>
  8. </ScrollView>

更多相关文章

  1. android之调用webservice 实现图片上传
  2. android 按钮设计中state_selected属性
  3. 图片缩放设置
  4. Android利用Fiddler进行网络数据抓包
  5. android 实现图片上传功能 Tomcat作为服务器
  6. Android(安卓): 一键分享(纲领)
  7. ListActivity中android:id="@+id/android:list"是啥玩意儿?
  8. Android之使用Android-query框架进行开发(一)
  9. Android-Module:ImageView常用XML属性

随机推荐

  1. wordpress登录失败处理user-lock插件安装
  2. 运维少年系列 python and cisco (1)
  3. 3-11(栈和队列的相互实现)
  4. 运维少年系列 - python and cisco(2)
  5. 10:VMware Horizon View 8.0-证书配置,解决
  6. 运维少年系列 python and cisco (3)
  7. redis高可用集群架构总结
  8. Linux轻量级监控软件-nmon
  9. 今天双十一,给大家送几套视频教程吧
  10. shell 双多分支if条件句语法