一:查看网络图片
1、用tomcat跑起一个web项目,记下图片路径:
这里的ip切记不要写成localhost或者127.0.0.1 ,写成局域网的。
2、搭建android项目
涉及的文件如下图红框内
(1)、MainActivity
/netimage/src/net/thinkeye/image/ImageMainActivity.java
- package net.thinkeye.image;
- import net.thinkeye.service.ImageService;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class ImageMainActivity extends Activity {
- private EditText pathText;
- private ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.image_main);
- pathText = (EditText) this.findViewById(R.id.imagepath);
- imageView = (ImageView) this.findViewById(R.id.imageView);
- Button b = (Button)this.findViewById(R.id.button);
- b.setOnClickListener(new ButtonClickListener());
- }
- private final class ButtonClickListener implements View.OnClickListener{
- @Override
- public void onClick(View v) {
- String path = pathText.getText().toString();
- try {
- byte[] data = ImageService.getImage(path);
- Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
- imageView.setImageBitmap(bitmap);//显示图片
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), R.string.error, 1).show();
- }
- }
- }
- }
2、
/netimage/src/net/thinkeye/service/ImageService.java
- package net.thinkeye.service;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import net.thinkeye.utils.StreamTool;
- public class ImageService {
- /**
- * 获取网络图片的数据
- * @param path
- * @return
- */
- public static byte[] getImage(String path) throws Exception{
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if(conn.getResponseCode() == 200){
- InputStream is = conn.getInputStream();
- return StreamTool.read(is);
- }
- return null;
- }
- }
3、/netimage/src/net/thinkeye/utils/StreamTool.java
- package net.thinkeye.utils;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- public class StreamTool {
- /**
- * 读取流中的数据
- * @param is
- * @return
- * @throws Exception
- */
- public static byte[] read(InputStream is) throws Exception{
- ByteArrayOutputStream o = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while((len = is.read(buffer)) != -1){
- o.write(buffer,0,len);
- }
- is.close();
- return o.toByteArray();
- }
- }
4、页面布局部分:
image_main.xml
- <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="@string/imagepath" />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="http://192.168.1.103:8888/SAMS/2.gif"
- android:id="@+id/imagepath" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button"
- android:id="@+id/button" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"/>
- </LinearLayout>
5、定义字符常量
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">网络图片查看器</string>
- <string name="imagepath">网络图片路径</string>
- <string name="button">查看</string>
- <string name="error">获取图片失败</string>
- </resources>
6、在AndroidManifest.xml文件里加入访问网络的权限,因为这里涉及到上网流量,会向用户收取一定的费用,在默认情况下是没有这个权限的,需要在此文件中申请权限,如下图:
好了,tomcat跑起来后在浏览器中能看到图片后,就把这个应用部署到模拟器上了。
部署完成后,就到了如下的页面:
点击上图的 查看 按钮,即可查看网络中的图片
二:查看页面源代码的功能和这个差不多,如下图:
下面显示源代码的部分的TextView需用ScrollView包裹起来,
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/codeView" />
- </ScrollView>
本文出自 “新博客-http://cfei.net” 博客,请务必保留此出处http://johnny84.blog.51cto.com/2855387/1162457