中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

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

松阳县| 车致| 沧源| 舟山市| 疏附县| 临高县| 苗栗市| 吉首市| 湟中县| 乐陵市| 金堂县| 永寿县| 长宁区| 昌乐县| 祁连县| 平安县| 龙州县| 渭源县| 黔西县| 思南县| 大余县| 湖口县| 祁门县| 涞源县| 崇州市| 峡江县| 泽普县| 会昌县| 汉沽区| 中方县| 女性| 汤阴县| 抚顺市| 镇安县| 射阳县| 屏东市| 庐江县| 南川市| 曲松县| 黄陵县| 曲麻莱县|