查看网络图片

思路:通过httpConnection获取文件流,将文件流转为字节数组,将数组装换为位图付给imageView  

清单文件中加入<uses-permission android:name="android.permission.INTERNET"/>

imageServise.java

package com.example.seepicture.servise;



import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;



public class imageServise {



    /**

     * 获取网络图片的数据

     * @return

     * @throws IOException 

     */

    public static byte[] getImg(String path) throws IOException{

        URL url=new URL(path);

        InputStream inStreams = null;

        HttpURLConnection connection=(HttpURLConnection) url.openConnection();//得到基于http协议的链接对象

        connection.setConnectTimeout(5000);

        //设置get请求方式

        connection.setRequestMethod("GET");

        if(connection.getResponseCode()==200){

            inStreams=connection.getInputStream();

            return streamTools.streamToByte(inStreams);

        }

        return null;

        

    }

}
sreamTools.java

package com.example.seepicture.servise;



import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;



public class streamTools {



    /**

     * 将流中的数据读取到字节数组中

     * @param inStream

     * @return

     * @throws IOException

     */

    public static byte[] streamToByte(InputStream inStream) throws IOException{

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len = 0;

        while( (len = inStream.read(buffer)) != -1){

            outStream.write(buffer, 0, len);

        }

        inStream.close();

        return outStream.toByteArray();

    }

}
MainActivity.java

package com.example.seepicture;



import java.io.IOException;

import java.io.InputStream;

import java.security.PublicKey;





import com.example.seepicture.servise.imageServise;







import android.os.Bundle;

import android.os.Looper;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.view.Menu;

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 {



    private EditText txt_path;

    private Button btn_see;

    private ImageView imgView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        txt_path=(EditText) findViewById(R.id.txt_path);

        btn_see=(Button) findViewById(R.id.btn_see);

        imgView=(ImageView) findViewById(R.id.img);

        btn_see.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                addImg();

            }

        });

    }



    

    /**

     * 按钮点击事件

     * @param view

     */

    private void addImg(){

        String path=txt_path.getText().toString();

        byte[] data = null;

        try {

            if(path.equals("")){

                data = imageServise.getImg("http://pica.nipic.com/2008-06-13/2008613145022997_2.jpg");

            }else{

                data = imageServise.getImg(path);

            }

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

            imgView.setImageBitmap(bitmap);//显示图片

        

        } catch (IOException e) {

            Toast.makeText(getApplicationContext(), "数据请求错误,图片路径不正确", 1).show();

            

        }

        

    }

}
布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >



    <TextView 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="网络图片路径"

        />

    <EditText 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/txt_path"

        />

    <Button 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/btn_see"

        android:onClick="addImg"

        android:text="获取图片"

        />

    <ImageView 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/img"

        />

</LinearLayout>

 

posted @ 2015-08-20 17:10  Jason_Msbaby  阅读(306)  评论(0编辑  收藏  举报