最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android互聯(lián)網(wǎng)訪問圖片并在客戶端顯示的方法

 更新時間:2015年12月26日 11:57:34   作者:sgx425021234  
這篇文章主要介紹了Android互聯(lián)網(wǎng)訪問圖片并在客戶端顯示的方法,結(jié)合實例分析了Android處理圖片的技巧,并附帶了Android的URL封裝類,網(wǎng)絡(luò)連接封裝類與輸出流封裝類,需要的朋友可以參考下

本文實例講述了Android互聯(lián)網(wǎng)訪問圖片并在客戶端顯示的方法。分享給大家供大家參考,具體如下:

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)的一些類

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è)置訪問的url
   URL url=new URL(path);
   //打開請求
   HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
   //設(shè)置請求的信息
   httpURLConnection.setRequestMethod("GET");
   //設(shè)置請求是否超時
   httpURLConnection.setConnectTimeout(5000);
   //判斷服務(wù)器是否響應(yīng)成功
   if(httpURLConnection.getResponseCode()==200){
    //獲取響應(yīng)的輸入流對象
    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");
    }
   }
   //斷開連接
   httpURLConnection.disconnect();
  }catch(Exception e){
   e.printStackTrace();
  }
  return content;
 }
 public static Bitmap sendGets(String path){
  Bitmap bitmap=null;
  try{
   //設(shè)置訪問的url
   URL url=new URL(path);
   //打開請求
   HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
   //設(shè)置請求的信息
   httpURLConnection.setRequestMethod("GET");
   //設(shè)置請求是否超時
   httpURLConnection.setConnectTimeout(5000);
   //判斷服務(wù)器是否響應(yīng)成功
   if(httpURLConnection.getResponseCode()==200){
    //獲取響應(yīng)的輸入流對象
    InputStream is=httpURLConnection.getInputStream();
    //直接把is的流轉(zhuǎn)換成Bitmap對象
    bitmap=BitmapFactory.decodeStream(is);
   }
   //斷開連接
   httpURLConnection.disconnect();
  }catch(Exception e){
   e.printStackTrace();
  }
  return bitmap;
 }
}

判斷網(wǎng)絡(luò)是否連接的封裝類

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)路鏈接管理對象
 public ConnectivityManager connectivityManager;
 public NetWorkUtils(Context context) {
  this.context = context;
  // 獲取網(wǎng)絡(luò)鏈接的對象
  connectivityManager = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
 }
 public boolean setActiveNetWork() {
  boolean flag=false;
  // 獲取可用的網(wǎng)絡(luò)鏈接對象
  NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  if (networkInfo == null) {
   new AlertDialog.Builder(context)
     .setTitle("網(wǎng)絡(luò)不可用")
     .setMessage("可以設(shè)置網(wǎng)絡(luò)?")
     .setPositiveButton("確認",
       new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog,
          int which) {
         Toast.makeText(context, "點擊確認",
           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;
 }
}

輸出流的封裝類

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];
  //讀取記錄的長度
  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)限問題

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

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android開發(fā)之動畫實現(xiàn)方法

    Android開發(fā)之動畫實現(xiàn)方法

    這篇文章主要介紹了Android開發(fā)之動畫實現(xiàn)方法,實例分析了Android中動畫的原理與實現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • Android單選按鈕RadioButton的使用詳解

    Android單選按鈕RadioButton的使用詳解

    今天小編就為大家分享一篇關(guān)于Android單選按鈕RadioButton的使用詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 詳細分析Android-Zygote的啟動過程

    詳細分析Android-Zygote的啟動過程

    在Android系統(tǒng)中,所有的應(yīng)用程序進程以及系統(tǒng)服務(wù)進程SystemServer都是由Zygote進程孕育(fork)出來的,這也許就是為什么要把它稱為Zygote(受精卵)的原因吧。由于Zygote進程在Android系統(tǒng)中有著如此重要的地位,本文將詳細分析它的啟動過程
    2021-06-06
  • RecyclerView上拉加載封裝代碼

    RecyclerView上拉加載封裝代碼

    這篇文章主要為大家詳細介紹了RecyclerView上拉加載封裝代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法

    詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法

    這篇文章主要介紹了詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Android RecyclerView實現(xiàn)懸浮吸頂、分隔線、到底提示效果

    Android RecyclerView實現(xiàn)懸浮吸頂、分隔線、到底提示效果

    這篇文章主要介紹了Android RecyclerView實現(xiàn)懸浮吸頂、分隔線、到底提示效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例

    Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例

    這篇文章主要介紹了Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能,結(jié)合簡單實例形式分析了Android使用CheckBox控件的布局與功能實現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • 詳解Dagger2在Android開發(fā)中的新用法

    詳解Dagger2在Android開發(fā)中的新用法

    本篇文章主要介紹了Dagger2在Android開發(fā)中的新用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Android TabWidget底部顯示效果

    Android TabWidget底部顯示效果

    這篇文章主要為大家詳細介紹了Android TabWidget底部顯示效果的三種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 淺析Android手機衛(wèi)士關(guān)閉自動更新

    淺析Android手機衛(wèi)士關(guān)閉自動更新

    保存數(shù)據(jù)的四種方式,網(wǎng)絡(luò),廣播提供者,SharedPreferences,數(shù)據(jù)庫。接下來通過本文給大家介紹android手機衛(wèi)士關(guān)閉自動更新的相關(guān)知識,感興趣的朋友一起學習吧
    2016-04-04

最新評論

夏邑县| 衡阳市| 清徐县| 福安市| 凤冈县| 南溪县| 上思县| 随州市| 东兰县| 凤山县| 新建县| 唐海县| 美姑县| 武汉市| 同江市| 吴旗县| 宜阳县| 巴林右旗| 屯门区| 黄平县| 玉环县| 盐山县| 屯昌县| 宝山区| 余庆县| 大石桥市| 中卫市| 西乌珠穆沁旗| 德化县| 桂阳县| 泰安市| 岗巴县| 温州市| 军事| 博罗县| 奇台县| 孟州市| 米易县| 玛曲县| 汽车| 海伦市|