Android开发入门之网络通信(网络图片查看器)

第一步:新建一个Android工程命名为netimage目录结构如下图:

Android开发入门之网络通信(网络图片查看器)_第1张图片


第二步:修改activity_main.xml布局文件代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/imagepath" />

    <EditText
        android:id="@+id/et_imagepath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/path" />

    <Button
        android:id="@+id/btn_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/view" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/>

</LinearLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">网络图片查看器</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="imagepath">网络图片路径</string>
    <string name="view">查看</string>
    <string name="success">获取图片成功!</string>
    <string name="fail">获取图片失败!</string>
    <string name="path">http://p1.qhimg.com/d/_onebox/search.png</string>

</resources>

第三步:编写MianActivity类:

package cn.leigo.netimage;

import java.io.IOException;

import cn.leigo.service.ImageService;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private EditText mImagePathEditText;
	private Button mViewButton;
	private ImageView mImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mImagePathEditText = (EditText) findViewById(R.id.et_imagepath);
		mViewButton = (Button) findViewById(R.id.btn_view);
		mImageView = (ImageView) findViewById(R.id.iv);
		mViewButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String imagePath = mImagePathEditText.getText().toString();
		byte[] data;
		try {
			data = ImageService.getImage(imagePath);
			Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			mImageView.setImageBitmap(bitmap); // 显示图片
			Toast.makeText(this, R.string.success, Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			e.printStackTrace();
			Toast.makeText(this, R.string.fail, Toast.LENGTH_SHORT).show();
		}

	}

}

业务类ImageService:

package cn.leigo.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import cn.leigo.utils.StreamTool;

public class ImageService {

	/**
	 * 获取网络图片的数据
	 * 
	 * @param imagePath
	 *            网络图片路径
	 * @return
	 * @throws IOException
	 */
	public static byte[] getImage(String imagePath) throws IOException {
		byte[] data = null;
		URL url = new URL(imagePath);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 基于HTTP协议链接对象
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			InputStream inputStream = conn.getInputStream();
			data = StreamTool.read(inputStream);
		}
		return data;
	}

}

工具类StreamTool:
package cn.leigo.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTool {

	/**
	 * 读取流中的数据
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static byte[] read(InputStream inputStream) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inputStream.read(buffer)) != -1) {
			baos.write(buffer, 0, len);
		}
		inputStream.close();
		return baos.toByteArray();
	}

}

最后别忘了在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET" />

运行上述工程查看效果图:

Android开发入门之网络通信(网络图片查看器)_第2张图片

你可能感兴趣的:(android,网络,图片查看器)