Android互聯(lián)網(wǎng)訪(fǎng)問(wèn)圖片并在客戶(hù)端顯示的方法
本文實(shí)例講述了Android互聯(lián)網(wǎng)訪(fǎng)問(wèn)圖片并在客戶(hù)端顯示的方法。分享給大家供大家參考,具體如下:
1、布局界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/url_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:ems="10" android:inputType="textPostalAddress" android:text="@string/url_text" > <requestFocus /> </EditText> <Button android:id="@+id/btn_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/url_text" android:layout_below="@+id/url_text" android:layout_marginTop="32dp" android:onClick="sendHttp" android:text="@string/btn_text" /> <ImageView android:id="@+id/iv_ie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/url_text" android:layout_below="@+id/btn_text" android:src="@drawable/ic_launcher" /> </RelativeLayout>
2、封轉(zhuǎn)的一些類(lèi)
URL的封裝:
package com.example.lession08_code.utis; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class HttpUtils { public static String sendGet(String path){ String content=null; try{ //設(shè)置訪(fǎng)問(wèn)的url URL url=new URL(path); //打開(kāi)請(qǐng)求 HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection(); //設(shè)置請(qǐng)求的信息 httpURLConnection.setRequestMethod("GET"); //設(shè)置請(qǐng)求是否超時(shí) httpURLConnection.setConnectTimeout(5000); //判斷服務(wù)器是否響應(yīng)成功 if(httpURLConnection.getResponseCode()==200){ //獲取響應(yīng)的輸入流對(duì)象 InputStream is=httpURLConnection.getInputStream(); byte data[]=StreamTools.isTodata(is); //把轉(zhuǎn)換成字符串 content=new String(data); //內(nèi)容編碼方式 if(content.contains("gb2312")){ content=new String(data,"gb2312"); } } //斷開(kāi)連接 httpURLConnection.disconnect(); }catch(Exception e){ e.printStackTrace(); } return content; } public static Bitmap sendGets(String path){ Bitmap bitmap=null; try{ //設(shè)置訪(fǎng)問(wèn)的url URL url=new URL(path); //打開(kāi)請(qǐng)求 HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection(); //設(shè)置請(qǐng)求的信息 httpURLConnection.setRequestMethod("GET"); //設(shè)置請(qǐng)求是否超時(shí) httpURLConnection.setConnectTimeout(5000); //判斷服務(wù)器是否響應(yīng)成功 if(httpURLConnection.getResponseCode()==200){ //獲取響應(yīng)的輸入流對(duì)象 InputStream is=httpURLConnection.getInputStream(); //直接把is的流轉(zhuǎn)換成Bitmap對(duì)象 bitmap=BitmapFactory.decodeStream(is); } //斷開(kāi)連接 httpURLConnection.disconnect(); }catch(Exception e){ e.printStackTrace(); } return bitmap; } }
判斷網(wǎng)絡(luò)是否連接的封裝類(lèi)
package com.example.lession08_code.utis; import android.app.AlertDialog; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.widget.Toast; public class NetWorkUtils { private Context context; // 網(wǎng)路鏈接管理對(duì)象 public ConnectivityManager connectivityManager; public NetWorkUtils(Context context) { this.context = context; // 獲取網(wǎng)絡(luò)鏈接的對(duì)象 connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); } public boolean setActiveNetWork() { boolean flag=false; // 獲取可用的網(wǎng)絡(luò)鏈接對(duì)象 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { new AlertDialog.Builder(context) .setTitle("網(wǎng)絡(luò)不可用") .setMessage("可以設(shè)置網(wǎng)絡(luò)?") .setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "點(diǎn)擊確認(rèn)", Toast.LENGTH_LONG).show(); // 聲明意圖 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory("android.intent.category.LAUNCHER"); intent.setComponent(new ComponentName( "com.android.settings", "com.android.settings.Settings")); intent.setFlags(0x10200000); // 執(zhí)行意圖 context.startActivity(intent); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show();// 必須.show(); } if(networkInfo!=null){ flag=true; } return flag; } }
輸出流的封裝類(lèi)
package com.example.lession08_code.utis; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTools { public static byte[] isTodata(InputStream is) throws IOException{ //字節(jié)輸出流 ByteArrayOutputStream bops=new ByteArrayOutputStream(); //讀取數(shù)據(jù)的緩沖區(qū) byte buffer[]=new byte[1024]; //讀取記錄的長(zhǎng)度 int len=0; while((len=is.read(buffer))!=-1){ bops.write(buffer, 0, len); } //把讀取的內(nèi)容轉(zhuǎn)換成byte數(shù)組 byte data[]=bops.toByteArray(); return data; } }
注意:在這里還需要加權(quán)限問(wèn)題
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- nginx配置訪(fǎng)問(wèn)圖片路徑以及html靜態(tài)頁(yè)面的調(diào)取方法
- php如何控制用戶(hù)對(duì)圖片的訪(fǎng)問(wèn) PHP禁止圖片盜鏈
- SpringMVC上傳圖片與訪(fǎng)問(wèn)
- PHP在Windows IIS上傳的圖片無(wú)法訪(fǎng)問(wèn)的解決方法
- 使用AngularJS 應(yīng)用訪(fǎng)問(wèn) Android 手機(jī)的圖片庫(kù)
- 解決jsp頁(yè)面使用網(wǎng)絡(luò)路徑訪(fǎng)問(wèn)圖片的亂碼問(wèn)題
- js 利用image對(duì)象實(shí)現(xiàn)圖片的預(yù)加載提高訪(fǎng)問(wèn)速度
- Asp.Net二級(jí)域名共享Forms身份驗(yàn)證、下載站/圖片站的授權(quán)訪(fǎng)問(wèn)控制
- 輕松創(chuàng)建nodejs服務(wù)器(10):處理上傳圖片
- nodejs和php實(shí)現(xiàn)圖片訪(fǎng)問(wèn)實(shí)時(shí)處理
相關(guān)文章
gradle tool升級(jí)到3.0注意事項(xiàng)小結(jié)
這篇文章主要介紹了gradle tool升級(jí)到3.0注意事項(xiàng)及修改相關(guān)文件介紹,需要的朋友可以參考下2018-02-02Android 四種動(dòng)畫(huà)效果的調(diào)用實(shí)現(xiàn)代碼
在這里, 我將每種動(dòng)畫(huà)分別應(yīng)用于四個(gè)按鈕為例,需要的朋友可以參考下2013-01-01Android 在子線(xiàn)程中更新UI的幾種方法示例
本篇文章主要介紹了Android 在子線(xiàn)程中更新UI的幾種方法示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08Android 繪制多級(jí)樹(shù)形選擇列表實(shí)例代碼
這篇文章主要介紹了Android 繪制多級(jí)樹(shù)形選擇列表的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02Android自定View流式布局根據(jù)文字?jǐn)?shù)量換行
這篇文章主要為大家詳細(xì)介紹了Android自定View流式布局,根據(jù)文字?jǐn)?shù)量換行,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android實(shí)現(xiàn)TV端大圖瀏覽效果的全過(guò)程
最近的開(kāi)發(fā)中遇到了個(gè)需求,需要在tv端加載很長(zhǎng)的圖片,發(fā)現(xiàn)網(wǎng)上沒(méi)有相關(guān)的資料,所以跟大家分享下,這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid實(shí)現(xiàn)TV端大圖瀏覽效果的相關(guān)資料,需要的朋友可以參考下2023-01-01Android添加用戶(hù)組及自定義App權(quán)限的方法
今天小編就為大家分享一篇Android添加用戶(hù)組及自定義App權(quán)限的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Android程序報(bào)錯(cuò)程序包org.apache.http不存在問(wèn)題的解決方法
這篇文章主要介紹了Android程序報(bào)錯(cuò)"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問(wèn)題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06Android SQLite數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)優(yōu)化的方法
這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid SQLite數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)優(yōu)化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11