zoukankan      html  css  js  c++  java
  • Android入门:查看服务器图片应用

    一、网络图片查看器需求

     存在一个Web服务器,其中存在一个图片,在Android客户端能够访问这张图片并在Android客户端显示;

    当点击“提交”后,则会显示指定服务器的图片;

    需要注意的一点是:我们不能使用localhost表示本机,而需要使用局域网的IP地址,否则会抛Connection confused异常;

    二、核心代码介绍

     在AndroidManifest.xml中加入:

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

    (1)URL url = new URL("http://.....");   //将字符串转为URL类型

    (2)HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    (3)conn.setRequestMethod("GET");     //设置请求方法,如GET POST

    (4)conn.setReadTimeout(milliseconds);    //设置读超时时间

    (5)int code = conn.getResponseCode();      //获得响应码,如200表示OK,404表示无资源

    (6)InputStream in = conn.getInputStream();   //获得输入流

    (7)Bitmap bitmap = BitmapFactory.decodeByteArray(byte[]data,int begin,int length);   // 根据byte[] 转变为位图

    三、全部代码

    搭建Web服务器的过程我就忽略了,此处我们使用最常用的Tomcat,版本为7.0.6;

    MainActivity.java

    1. package org.xiazdong.view.image;  
    2.   
    3. import java.io.ByteArrayOutputStream;  
    4. import java.io.IOException;  
    5. import java.io.InputStream;  
    6. import java.net.HttpURLConnection;  
    7. import java.net.URL;  
    8.   
    9. import android.app.Activity;  
    10. import android.graphics.Bitmap;  
    11. import android.graphics.BitmapFactory;  
    12. import android.os.Bundle;  
    13. import android.view.View;  
    14. import android.view.View.OnClickListener;  
    15. import android.widget.Button;  
    16. import android.widget.EditText;  
    17. import android.widget.ImageView;  
    18. import android.widget.Toast;  
    19.   
    20. public class MainActivity extends Activity {  
    21.     private EditText editText;  
    22.     private Button button;  
    23.     private ImageView imageView;  
    24.     private OnClickListener listener = new OnClickListener(){  
    25.   
    26.         @Override  
    27.         public void onClick(View v) {  
    28.             Bitmap bitmap = null;  
    29.             try {  
    30.                 bitmap = getImage(editText.getText().toString());  
    31.             } catch (Exception e) {  
    32.                 e.printStackTrace();  
    33.                 Toast.makeText(MainActivity.this"获取图片失败",Toast.LENGTH_SHORT).show();  
    34.             }  
    35.             if(bitmap!=null)  
    36.                 imageView.setImageBitmap(bitmap);  
    37.             else{  
    38.                 Toast.makeText(MainActivity.this"获取图片失败",Toast.LENGTH_SHORT).show();  
    39.             }  
    40.         }  
    41.   
    42.         private Bitmap getImage(String path) throws Exception {  
    43.             URL url = new URL(path);  
    44.             HttpURLConnection con = (HttpURLConnection) url.openConnection();  
    45.             byte[]data ;  
    46.             con.setRequestMethod("GET");  
    47.             if(con.getResponseCode()==200){  
    48.                 InputStream in = con.getInputStream();  
    49.                 data = read2Byte(in);  
    50.                 return BitmapFactory.decodeByteArray(data, 0, data.length);  
    51.             }  
    52.             else return null;  
    53.         }  
    54.     };  
    55.     @Override  
    56.     public void onCreate(Bundle savedInstanceState) {  
    57.         super.onCreate(savedInstanceState);  
    58.         setContentView(R.layout.main);  

     StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
             .detectDiskReads() 
             .detectDiskWrites() 
             .detectNetwork()   // or .detectAll() for all detectable problems 
             .penaltyLog() 
             .build()); 
           StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 
             .detectLeakedSqlLiteObjects() 
             .detectLeakedClosableObjects() 
             .penaltyLog() 
             .penaltyDeath() 
             .build()); 

    1.         editText = (EditText)this.findViewById(R.id.imagepath);  
    2.         button = (Button)this.findViewById(R.id.button);  
    3.         imageView = (ImageView)this.findViewById(R.id.imageview);  
    4.         button.setOnClickListener(listener);  
    5.     }  
    6.     private byte[] read2Byte(InputStream in) throws IOException {  
    7.         byte[] data;  
    8.         ByteArrayOutputStream bout = new ByteArrayOutputStream();  
    9.         byte[]buf = new byte[1024];  
    10.         int len = 0;  
    11.         while((len = in.read(buf))!=-1){  
    12.             bout.write(buf, 0, len);  
    13.         }  
    14.         data = bout.toByteArray();  
    15.         return data;  
    16.     }  
    17. }  

    main.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <TextView  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content"  
    10.         android:text="图片路径"  
    11.          />  
    12.     <!-- 此处不能用localhost,一定要用ip地址 -->  
    13.     <EditText   
    14.         android:id="@+id/imagepath"  
    15.          android:layout_width="fill_parent"  
    16.         android:layout_height="wrap_content"  
    17.         android:text="http://192.168.0.103:8080/Server/logo.png"   
    18.         />  
    19.     <Button   
    20.         android:id="@+id/button"  
    21.          android:layout_width="wrap_content"  
    22.         android:layout_height="wrap_content"  
    23.         android:text="提交"  
    24.         />  
    25.     <ImageView   
    26.         android:layout_width="wrap_content"  
    27.         android:layout_height="wrap_content"  
    28.         android:id="@+id/imageview"  
    29.         />  
    30.   
    31. </LinearLayout>  

    转:http://www.linuxidc.com/Linux/2012-07/64599.htm

     

  • 相关阅读:
    服务器资源共享--IIS站点/虚拟目录中访问共享目录(UNC)
    sql reiserror 输出错误
    使用xib方式创建UITableViewCell,设置Label自动换行注意事项
    原生的UITableViewCell高度自适应,textLabel自动换行显示
    屏幕截取-2种模式
    NSDictionary初始化,使用@{}方法,插入nil时会报空指针异常
    Unicode解码、URL编码/解码
    解决UITableView数据没有充满屏幕时,显示多余的空白cell的问题
    UITableView的分割线不满屏的解决方法
    动态获取UIWebView的真正高度
  • 原文地址:https://www.cnblogs.com/licomeback/p/3457154.html
Copyright © 2011-2022 走看看