Android学习笔记_12_网络通信之从web获取资源数据到Android

    从web获取图片信息,并显示到android的imageView控件。

  一、添加网络访问权限。

<uses-permission android:name="android.permission.INTERNET"/>

  二、界面布局及activity的实现:

<RelativeLayout 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" >



    <Button

        android:id="@+id/btnImage"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="34dp"

        android:layout_marginTop="15dp"

        android:text="查看图片" />



    <ImageView

        android:id="@+id/imageView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/btnImage"

        android:layout_below="@+id/btnImage"

        android:layout_marginTop="20dp"

         />



</RelativeLayout>

 

package com.example.image;



import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;



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 {

    private Button button = null;

    private ImageView imageView;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button = (Button) this.findViewById(R.id.btnImage);

        imageView = (ImageView) this.findViewById(R.id.imageView);

        final String path="http://192.168.8.103:8080/Simple/0001.jpg";

        button.setOnClickListener(new OnClickListener() {        

            @Override

            public void onClick(View view) {

                try {

                    byte[] data = getImage(path);

                    Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);

                    imageView.setImageBitmap(bm);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });        

    }

    /**

     * 获取图片流

     * @param path

     * @return

     * @throws Exception

     */

    public static byte[] getImage(String path) throws Exception{

 //创建URL对象,获取HttpURLConnection
HttpURLConnection conn
= (HttpURLConnection) new URL(path).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode()==200) { //处理从web返回的资源数据 return read(conn.getInputStream()); } return null; } /** * 将流转化成字节数组 * @param in * @return * @throws Exception */ public static byte[] read(InputStream in) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len=0; while((len=in.read(buf))!=-1){ outputStream.write(buf,0,len); } outputStream.close(); return outputStream.toByteArray(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }

 

你可能感兴趣的:(Android学习)