`
01jiangwei01
  • 浏览: 532523 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

安卓课程二十四 ImageView从网络上获取图片

 
阅读更多

本节采用两种方法创建Bitmap对象。主要过程是在xml中创建两个组件ImageView和Button。点击按钮下载图片,并做显示。创建了工具类HttpUtils,下载使用。

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    >

    <Button android:id="@+id/button_"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/downImg"
       />
    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"/>
     
</LinearLayout>

 HttpUtils.java

package com.example.imageviewhttp;

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

public class HttpUtils {

	private final static String URL_PATH = "http://192.168.78.1:8081/web/pic/logo_2.jpg";//访问网络图片的路径
	public HttpUtils() {
	}
	/**
	 * 从网络中获取图片信息,以流的形式返回
	 * @return
	 */
	public static InputStream getImageViewInputStream(){
		InputStream inputStream = null;
		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 resonpseCode = httpURLConnection.getResponseCode();
				if(resonpseCode == 200){
					inputStream = httpURLConnection.getInputStream();
				}
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		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 resonpseCode = httpURLConnection.getResponseCode();
				int len = 0;
				byte[] b_data = new byte[1024];
				if(resonpseCode == 200){
					inputStream = httpURLConnection.getInputStream();
					;
					while ((len =inputStream.read(b_data)) !=-1){
						outputStream.write(b_data, 0, len);
					}
					data = outputStream.toByteArray();
				}
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(inputStream!=null){try {
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}}
		}
		return data;
	}
}

 MainActivity.java

import java.io.InputStream;

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

public class MainActivity extends Activity  implements OnClickListener{
	private ImageView imageview;
	private Button button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageview = (ImageView) this.findViewById(R.id.imageView);
		button = (Button) this.findViewById(R.id.button_);
		button.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onClick(View v) {
//	 方法一
//		InputStream inputStream = HttpUtils.getImageViewInputStream();
//		Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//	方法二
		byte[] data = HttpUtils.getImageViewArray();
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		imageview.setImageBitmap(bitmap);
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics