歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android入門:查看服務器圖片應用

Android入門:查看服務器圖片應用

日期:2017/3/1 10:17:40   编辑:Linux編程

一、網絡圖片查看器需求

存在一個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. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import android.app.Activity;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.ImageView;
  16. import android.widget.Toast;
  17. public class MainActivity extends Activity {
  18. private EditText editText;
  19. private Button button;
  20. private ImageView imageView;
  21. private OnClickListener listener = new OnClickListener(){
  22. @Override
  23. public void onClick(View v) {
  24. Bitmap bitmap = null;
  25. try {
  26. bitmap = getImage(editText.getText().toString());
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. Toast.makeText(MainActivity.this, "獲取圖片失敗",Toast.LENGTH_SHORT).show();
  30. }
  31. if(bitmap!=null)
  32. imageView.setImageBitmap(bitmap);
  33. else{
  34. Toast.makeText(MainActivity.this, "獲取圖片失敗",Toast.LENGTH_SHORT).show();
  35. }
  36. }
  37. private Bitmap getImage(String path) throws Exception {
  38. URL url = new URL(path);
  39. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  40. byte[]data ;
  41. con.setRequestMethod("GET");
  42. if(con.getResponseCode()==200){
  43. InputStream in = con.getInputStream();
  44. data = read2Byte(in);
  45. return BitmapFactory.decodeByteArray(data, 0, data.length);
  46. }
  47. else return null;
  48. }
  49. };
  50. @Override
  51. public void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.main);
  54. editText = (EditText)this.findViewById(R.id.imagepath);
  55. button = (Button)this.findViewById(R.id.button);
  56. imageView = (ImageView)this.findViewById(R.id.imageview);
  57. button.setOnClickListener(listener);
  58. }
  59. private byte[] read2Byte(InputStream in) throws IOException {
  60. byte[] data;
  61. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  62. byte[]buf = new byte[1024];
  63. int len = 0;
  64. while((len = in.read(buf))!=-1){
  65. bout.write(buf, 0, len);
  66. }
  67. data = bout.toByteArray();
  68. return data;
  69. }
  70. }
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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="圖片路徑"
  10. />
  11. <!-- 此處不能用localhost,一定要用ip地址 -->
  12. <EditText
  13. android:id="@+id/imagepath"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="http://192.168.0.103:8080/Server/logo.png"
  17. />
  18. <Button
  19. android:id="@+id/button"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="提交"
  23. />
  24. <ImageView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:id="@+id/imageview"
  28. />
  29. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved