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

Android讀取服務(wù)器圖片的三種方法

 更新時(shí)間:2017年05月31日 08:39:54   作者:wanghualei2  
這篇文章主要為大家詳細(xì)介紹了Android讀取服務(wù)器圖片的三種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android鏈接服務(wù)器獲取圖片在此提供三種方法,已通過(guò)驗(yàn)證,無(wú)誤。

方法一:

public static Bitmap getImage(String path){ 
   
  try { 
    HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); 
    conn.setConnectTimeout(5000); 
    conn.setRequestMethod("GET"); 
    System.out.println("tdw1"); 
    if(conn.getResponseCode() == 200){ 
      InputStream inputStream = conn.getInputStream(); 
      Bitmap bitmap = BitmapFactory.decodeStream(inputStream);   
      return bitmap; 
    } 
  } catch (Exception e) { 
    e.printStackTrace(); 
  } 
  return null; 
} 

在第一種方法中,從conn的輸入流中獲取數(shù)據(jù)將其轉(zhuǎn)化為Bitmap型數(shù)據(jù)。

在功能代碼中:

image.setImageBitmap(getImage("路徑")); 

image為ImageView型控件。

第二種方法:

public static Bitmap getImage1(String path){ 
   
    HttpGet get = new HttpGet(path); 
    HttpClient client = new DefaultHttpClient(); 
    Bitmap pic = null; 
     try { 
      HttpResponse response = client.execute(get); 
      HttpEntity entity = response.getEntity(); 
      InputStream is = entity.getContent(); 
 
      pic = BitmapFactory.decodeStream(is);  // 關(guān)鍵是這句代 
  } catch (Exception e) { 
    e.printStackTrace(); 
  } 
  return pic; 
} 

這個(gè)方法類似上面那個(gè)方法。在功能代碼中設(shè)置是一樣的

第三種方法:

public static Uri getImage2(String path,File cacheDir){ 
    File localFile = new File(cacheDir,MD5.getMD5(path)+path.substring(path.lastIndexOf("."))); 
    if(localFile.exists()){ 
      return Uri.fromFile(localFile); 
    }else 
    { 
      HttpURLConnection conn; 
      try { 
        conn = (HttpURLConnection) new URL(path).openConnection(); 
        conn.setConnectTimeout(5000); 
        conn.setRequestMethod("GET"); 
        if(conn.getResponseCode() == 200){ 
          System.out.println("tdw"); 
          FileOutputStream outputStream = new FileOutputStream(localFile); 
          InputStream inputStream = conn.getInputStream(); 
          byte[] buffer = new byte[1024]; 
          int length = 0; 
          while((length=inputStream.read(buffer))!=-1){ 
            outputStream.write(buffer, 0, length); 
          } 
          inputStream.close(); 
          outputStream.close(); 
          return Uri.fromFile(localFile); 
        } 
      } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
    } 
    return null;   
  } 

第三種方法,將從服務(wù)器獲取的數(shù)據(jù)存入本地的文件中,如果文件已存在,則不需要從服務(wù)器重新獲取數(shù)據(jù)。
在功能代碼中:

image.setImageURI(getImage2(path, cache)); 

上面代碼中設(shè)置圖片為緩存設(shè)置,這樣如果圖片資源更新了,則需要重新命名文件的名字,這樣才能夠重新加載新圖片。

cache = new File(Environment.getExternalStorageDirectory(),"cache"); 
if(!cache.exists()){ 
  cache.mkdirs(); 
} 

這里是設(shè)置 緩存圖片的路徑。
以上為三種方法。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android 網(wǎng)絡(luò)連接處理分析

    android 網(wǎng)絡(luò)連接處理分析

    在Android中,可以有多種方式來(lái)實(shí)現(xiàn)網(wǎng)絡(luò)編程,本文將詳細(xì)介紹android 網(wǎng)絡(luò)連接處理,需要了解的朋友可以參考下
    2012-11-11
  • Android開(kāi)發(fā)數(shù)據(jù)結(jié)構(gòu)算法ArrayList源碼詳解

    Android開(kāi)發(fā)數(shù)據(jù)結(jié)構(gòu)算法ArrayList源碼詳解

    這篇文章主要為大家介紹了Android開(kāi)發(fā)數(shù)據(jù)結(jié)構(gòu)算法ArrayList源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Android搭建grpc環(huán)境過(guò)程分步詳解

    Android搭建grpc環(huán)境過(guò)程分步詳解

    本篇文章使用的IDE是Android Studio。這里先吐槽一句,安卓項(xiàng)目搭建grpc環(huán)境,不管是引入插件還是引入第三方庫(kù),對(duì)于版本的要求都極為苛刻,一旦版本不匹配就會(huì)報(bào)錯(cuò),所以對(duì)于版本的搭配一定要注意
    2023-04-04
  • 一文理解Android系統(tǒng)中強(qiáng)指針的實(shí)現(xiàn)

    一文理解Android系統(tǒng)中強(qiáng)指針的實(shí)現(xiàn)

    因?yàn)锳ndroid中很多地方代碼是用C++編寫,為了能夠保證C++中指針能夠被正確的釋放,于是Android引入了其實(shí)在C++中已經(jīng)有的智能指針技術(shù)
    2021-10-10
  • Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解

    Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解

    這篇文章主要為大家介紹了Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android HandlerThread使用方法詳解

    Android HandlerThread使用方法詳解

    這篇文章主要介紹了Android HandlerThread使用方法詳解的相關(guān)資料,通過(guò)本文希望能幫助到大家,讓大家掌握理解這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼

    Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼

    這篇文章主要介紹了Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • Bootstrap 下拉菜單.dropdown的具體使用方法

    Bootstrap 下拉菜單.dropdown的具體使用方法

    這篇文章主要介紹了Bootstrap 下拉菜單.dropdown的具體使用方法,詳細(xì)講解下拉菜單的交互,有興趣的可以了解一下
    2017-10-10
  • 詳解Android UI更新的幾種方法

    詳解Android UI更新的幾種方法

    本篇文章主要介紹了Android UI更新的幾種方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法(Http?Server)

    Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法(Http?Server)

    這篇文章主要介紹了Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法,包括資源的配置,資源的下載和存儲(chǔ),版本的管理,如何根據(jù)實(shí)際url獲取對(duì)應(yīng)HttpServer?bind的url等,需要的朋友可以參考下
    2022-05-05

最新評(píng)論

乌恰县| 资中县| 苍溪县| 班戈县| 揭东县| 乌拉特后旗| 河北省| 大同县| 余江县| 黔江区| 拉萨市| 新乡县| 大理市| 特克斯县| 昌乐县| 梅州市| 丘北县| 建昌县| 贵南县| 房产| 正阳县| 罗源县| 金沙县| 元氏县| 张家口市| 芷江| 太保市| 疏附县| 三河市| 乐业县| 苏尼特左旗| 万荣县| 随州市| 镇赉县| 兴海县| 宁陵县| 达拉特旗| 奇台县| 黑山县| 璧山县| 丹凤县|