android基础--网络图片查看器

 
package cn.com.netimage;

public class MainActivity extends Activity {

       private EditText imagepath;

       private ImageView imageView;

      

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        imagepath = (EditText) this.findViewById(R.id.imagepath);

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

        //找到按钮

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

        //为按钮增加点击事件

        button.setOnClickListener(new View.OnClickListener() {

                     public void onClick(View v) {

                            //得到路径

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

                            try {

                                   //获取图片数据

                                   byte [] data = ImageService.getImage(path);

                                   if(data !=null){

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

                                          //显示图片

                                          imageView.setImageBitmap(bitmap);

                                   }else{

                                          Toast.makeText(getApplicationContext(), R.string.error, 1).show();

                                   }

                            } catch (Exception e) {

                                   Toast.makeText(getApplicationContext(), R.string.error, 1).show();

                            }

                     }

              });

       

    }

}


package cn.com.service;

 

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import cn.com.utils.StreamTool;

public class ImageService {

       /**

        * 获取图片

        * @param path 网络图片路径

        * @return 图片的字节数据

        */

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

              //path包装成URL对象

              URL url = new URL(path);

              //打开基于http协议的链接

              HttpURLConnection connection = (HttpURLConnection) url.openConnection();

              //设置链接超时时间

              connection.setConnectTimeout(5000);

              //设置请求方式

              connection.setRequestMethod("GET");

              //得到输入流

              if(connection.getResponseCode()==200){//请求成功 

                       InputStream inStream = connection.getInputStream();

                       //做一个工具类来读取

                       byte[] data = StreamTool.read(inStream);

                       return data;

              }

              return null;

       }

}


package cn.com.utils;

 

public class StreamTool {

       /**

        * 读取输入流数据

        * @param inStream

        * @return

        * @throws IOException

        */

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

              ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

              byte [] buffer = new byte[1024];

              int len = 0;

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

                     outputStream .write(buffer, 0, len);

              }

              inStream.close();

              return outputStream.toByteArray();

       }

 

}


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

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/app_name"

    />

    <EditText

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="http://192.168.1.131:8080/videonews/a.jpg"

    android:id="@+id/imagepath"

    />

    <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/button"

    android:id="@+id/button"

    />

    <ImageView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:id="@+id/imageView"

    />

</LinearLayout>


设置访问权限

<!-- 访问internet权限 -->

   

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


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭