国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現網絡圖片瀏覽器

2019-12-12 02:51:43
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android網絡圖片瀏覽器的制作過程,供大家參考,具體內容如下

一、創建一個“網絡圖片瀏覽器的應用程序”,并設計用戶交互界面,“網絡圖片瀏覽器”對應的布局文件(activity_main.xml)代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bz0209.myapplication.MainActivity"> <ImageView android:layout_weight="1000" android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:singleLine="true" android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://b.hiphotos.baidu.com/image/w%3D310/sign=a439f5b24510b912bfc1f0fff3fdfcb5/83025aafa40f4bfb92c52c5d014f78f0f73618a5.jpg" android:hint="請輸入圖片路徑" /> <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="瀏覽" /></LinearLayout>

效果圖如下:

二、編寫界面交互代碼(MainActivity),當界面創建好后,需要在MainActivity里面編寫與界面交互的代碼。用于實現請求指定地址的網絡圖片,并將服務器返回的圖片展現在界面上。具體代碼如下:

package com.example.bz0209.myapplication;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;public class MainActivity extends AppCompatActivity { protected static final int CHANGE_UI=1; protected static final int ERROR=2; private EditText et_path; private ImageView iv; //主線創建消息處理器 private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg){  if (msg.what==CHANGE_UI){  Bitmap bitmap=(Bitmap)msg.obj;  iv.setImageBitmap(bitmap);  }else if (msg.what==ERROR){  Toast.makeText(MainActivity.this,"顯示圖片錯誤",0).show();  } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path=(EditText)findViewById(R.id.et_path); iv=(ImageView) findViewById(R.id.iv); } public void click(View view){ final String path=et_path.getText().toString().trim(); if (TextUtils.isEmpty(path)){  Toast.makeText(this,"圖片路徑不能為空",0).show(); } else {  //子線程請求網絡,Android 4.0以后訪問網絡不能放在子線程中  new Thread(){  private HttpURLConnection conn;  private Bitmap bitmap;  public void run(){   //鏈接服務器get請求,獲取圖片   try{   //創建URL對象   URL url =new URL(path);   conn=(HttpURLConnection) url.openConnection();   conn.setRequestMethod("GET");   conn.setConnectTimeout(5000);   conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatib;MSIE 6.0;Window NT 5.1;"+"SV1;" +    ".NET4.0C;.NET4.0E;NET CLK 2.0.50727;"+".NET CLR 3.0..4506.2152;.NET CLR 3.5.30729; Shuame )");   int code=conn.getResponseCode();   if(code==200){    InputStream is=conn.getInputStream();    bitmap= BitmapFactory.decodeStream(is);    Message msg=new Message();    msg.what=CHANGE_UI;    msg.obj=bitmap;    handler.sendMessage(msg);   }   else {    Message msg=new Message();    msg.what=ERROR;    handler.sendMessage(msg);   }   }catch (Exception e){   e.printStackTrace();   Message msg=new Message();   msg.what=ERROR;   }  }  }.start(); } }}

三、添加權限:由于網絡圖片瀏覽器需要請求網絡,因此需要在清單文件中配置相應的權限,具體操作如下圖所示:

四、運行瀏覽圖片 ,結果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 恩平市| 定结县| 开江县| 丹阳市| 芒康县| 昌黎县| 义马市| 股票| 新源县| 永春县| 韩城市| 花莲县| 五峰| 鹤庆县| 福州市| 灵石县| 康平县| 恩平市| 股票| 漾濞| 安仁县| 息烽县| 比如县| 海淀区| 济南市| 丘北县| 轮台县| 和田县| 和政县| 星座| 宣威市| 广东省| 陈巴尔虎旗| 永济市| 筠连县| 陇西县| 伊宁县| 和田市| 夏河县| 秭归县| 宁海县|