imageview加载网络图像

1.授权

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.marvinedittext2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        />
s
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<!-- 添加网络授权 -->
	<uses-permission 
    android:name="android.permission.INTERNET"></uses-permission>
	<uses-permission 
    android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
</manifest>


2.封装http工具(获取网络的信息,用流 的方式进行返回)

将流直接转换为一个字节数组,然后将这个数组直接转化为一个bitmap的对象.

package com.example.marvinedittext2;

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

public class HttpUtils {

	private final static String URL_PATH = "http://www.baidu.com/img/bdlogo.png";// 访问网路图片的路径

	public HttpUtils() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 从网络中获取图片信息,以流的形式返回
	 * 
	 * @return
	 */
	public static InputStream getImageViewInputStream() throws IOException {
		InputStream inputStream = null;

		URL url = new URL(URL_PATH);
		if (url != null) {
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			httpURLConnection.setConnectTimeout(3000);// 设置连接超时的时间
			httpURLConnection.setRequestMethod("GET");
			httpURLConnection.setDoInput(true);
			int response_code = httpURLConnection.getResponseCode();
			if (response_code == 200) {
				inputStream = httpURLConnection.getInputStream();
			}
		}
		return inputStream;
	}

	/**
	 * 从网络中获取图片信息,以字节数组的形式返回
	 * 
	 * @return
	 */
	public static byte[] getImageViewArray() {
		byte[] data = null;
		InputStream inputStream = null;
		// 不需要关闭的输出流,直接写入到内存中
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

		try {
			URL url = new URL(URL_PATH);
			if (url != null) {
				HttpURLConnection httpURLConnection = (HttpURLConnection) url
						.openConnection();
				httpURLConnection.setConnectTimeout(3000);// 设置连接超时的时间
				httpURLConnection.setRequestMethod("GET");// 请求方法
				httpURLConnection.setDoInput(true);// 打开输入流
				int response_code = httpURLConnection.getResponseCode();
				int len = 0;
				byte[] b_data = new byte[1024];

				if (response_code == 200) {
					inputStream = httpURLConnection.getInputStream();
					while ((len = inputStream.read(b_data)) != -1) {
						outputStream.write(b_data, 0, len);
					}
					data = outputStream.toByteArray();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return data;
	}
}


3.布局

<?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">
	<ImageView android:id="@+id/imageview" android:layout_width="wrap_content"
		android:layout_height="wrap_content" />

	<Button android:id="@+id/button" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="下载网络图片" />
</LinearLayout>


4.代码

package com.example.marvinedittext2;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Main extends Activity {
	/** Called when the activity is first created. */
	private Button button;
	private ImageView imageView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button = (Button) this.findViewById(R.id.button);
		imageView = (ImageView) this.findViewById(R.id.imageview);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// try {
				// InputStream inputStream = HttpUtils
				// .getImageViewInputStream();
				// Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
				// imageView.setImageBitmap(bitmap);
				// } catch (IOException e) {
				// // TODO: handle exception
				// }
				byte[] data = HttpUtils.getImageViewArray();
				Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
						data.length);
				imageView.setImageBitmap(bitmap);

			}
		});
	}
}



5.over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值