Android實現(xiàn)網(wǎng)絡圖片瀏覽器
本文實例為大家分享了Android網(wǎng)絡圖片瀏覽器的制作過程,供大家參考,具體內(nèi)容如下
一、創(chuàng)建一個“網(wǎng)絡圖片瀏覽器的應用程序”,并設計用戶交互界面,“網(wǎng)絡圖片瀏覽器”對應的布局文件(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),當界面創(chuàng)建好后,需要在MainActivity里面編寫與界面交互的代碼。用于實現(xiàn)請求指定地址的網(wǎng)絡圖片,并將服務器返回的圖片展現(xiàn)在界面上。具體代碼如下:
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; //主線創(chuàng)建消息處理器 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 { //子線程請求網(wǎng)絡,Android 4.0以后訪問網(wǎng)絡不能放在子線程中 new Thread(){ private HttpURLConnection conn; private Bitmap bitmap; public void run(){ //鏈接服務器get請求,獲取圖片 try{ //創(chuàng)建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(); } } }
三、添加權(quán)限:由于網(wǎng)絡圖片瀏覽器需要請求網(wǎng)絡,因此需要在清單文件中配置相應的權(quán)限,具體操作如下圖所示:
四、運行瀏覽圖片 ,結(jié)果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android ListView 單條刷新方法實踐及原理解析
這篇文章主要介紹了Android ListView 單條刷新方法實踐及原理解析的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Android監(jiān)控和阻斷InputDispatching ANR的方法
如何在Java層實現(xiàn)異步監(jiān)控和阻斷InputDispatching ANR?我相信這是很多開發(fā)者都想要的功能,本篇,我們會通過“探索”兩種方案來實現(xiàn)在Java層監(jiān)控&阻斷的方法,需要的朋友可以參考下2024-04-04Android實現(xiàn)網(wǎng)易云推薦歌單界面
大家好,本篇文章主要講的是Android實現(xiàn)網(wǎng)易云推薦歌單界面,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02如何在Android中實現(xiàn)一個簡易的Http服務器
這篇文章主要介紹了如何在Android中實現(xiàn)一個簡易的Http服務器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android使用Spinner實現(xiàn)城市級聯(lián)下拉框
這篇文章主要為大家詳細介紹了Android使用Spinner實現(xiàn)城市級聯(lián)下拉框,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android進階Hook攔截系統(tǒng)實例化View過程實現(xiàn)App換膚功能
這篇文章主要為大家介紹了Android進階Hook攔截系統(tǒng)實例化View過程實現(xiàn)App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01