91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android 中怎么利用Http下載文件到手機

發布時間:2021-06-29 14:10:38 來源:億速云 閱讀:564 作者:Leah 欄目:移動開發

Android 中怎么利用Http下載文件到手機,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

訪問Internet和保存文件到SDCard上,首先要在mainifest.xml文件中加上下面的權限。

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

gettextfilestring(String url)獲取文本文件內:

public String gettextfilestring(String url){                 InputStream input =getinputStream(url);                 StringBuffer sb = new StringBuffer("");                 BufferedReader bfr = new BufferedReader(new InputStreamReader(input));                 String line = "";                 try {                         while((line=bfr.readLine())!=null){                                 sb.append(line);                         }                                         } catch (IOException e) {                         toasterror("流文件讀寫錯誤");                         e.printStackTrace();                 }finally{                         try {                                 bfr.close();                         } catch (IOException e) {                                 toasterror("流文件未能正常關閉");                                 e.printStackTrace();                         }                 }                 return sb.toString(); }

downFiletoDecive(String url,String filename)方法下載文件到設備內存,下載的文件在應用的路徑file下:

public void downFiletoDecive(String url,String filename){                     if((url!=null&&!"".equals(url))&&(filename!=null&&!"".equals(filename))){                             InputStream input = getinputStream(url);                             FileOutputStream outStream = null;                             try {                                     outStream = c.openFileOutput(filename, Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);                                     int temp = 0;                                     byte[] data = new byte[1024];                                     while((temp = input.read(data))!=-1){                                             outStream.write(data, 0, temp);                                     }                             } catch (FileNotFoundException e) {                                     toasterror("請傳入正確的上下文");                                     e.printStackTrace();                             } catch (IOException e) {                                     toastemessage("讀寫錯誤");                                     e.printStackTrace();                             }finally{                                     try {                                             outStream.flush();                                             outStream.close();                                     } catch (IOException e) {                                             toasterror("流文件未能正常關閉");                                             e.printStackTrace();                                     }                                                                 }                     }                     toastemessage("下載成功");             }

downFiletoSDCard(String url,String path,String filename)下載文件到SDCard中,自定義保存路:

public void downFiletoSDCard(String url,String path,String filename){                                 if((url!=null&&!"".equals(url))&&(path!=null)&&(filename!=null&&!"".equals(filename))){                                                                 InputStream input = getinputStream(url);                                 downloader(input, path, filename);                                         }else{                                 /*                                  * 對不合發的參數做處理                                  */                                 if(url==null||"".equals(url)){                                         toasterror("url不能為空或為“”");                                 }                                 if(path==null){                                         toasterror("path不能為空");                                 }                                 if(filename==null||"".equals(filename)){                                         toasterror("filename不能為空");                                 }                 }                         }

關于Android 中怎么利用Http下載文件到手機問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

静乐县| 东光县| 石狮市| 梁河县| 教育| 汉源县| 民勤县| 永顺县| 即墨市| 满洲里市| 中西区| 安阳县| 临沂市| 凤阳县| 凯里市| 黑河市| 乡城县| 大城县| 东阿县| 神木县| 棋牌| 武邑县| 木兰县| 嘉定区| 商水县| 淅川县| 海安县| 丽水市| 虞城县| 定南县| 赫章县| 延长县| 南溪县| 监利县| 南通市| 靖远县| 兴仁县| 壶关县| 平顶山市| 廊坊市| 鞍山市|