標籤:byte new getc tar int bsp name color 操作
- //下載具體操作
- private void download() {
- try {
- URL url = new URL(downloadUrl);
- //開啟串連
- URLConnection conn = url.openConnection();
- //開啟輸入資料流
- InputStream is = conn.getInputStream();
- //獲得長度
- int contentLength = conn.getContentLength();
- Log.e(TAG, "contentLength = " + contentLength);
- //建立檔案夾 MyDownLoad,在儲存卡下
- String dirName = Environment.getExternalStorageDirectory() + "/MyDownLoad/";
- File file = new File(dirName);
- //不存在建立
- if (!file.exists()) {
- file.mkdir();
- }
- //下載後的檔案名稱
- String fileName = dirName + "xiaomibianqian.apk";
- File file1 = new File(fileName);
- if (file1.exists()) {
- file1.delete();
- }
- //建立位元組流
- byte[] bs = new byte[1024];
- int len;
- OutputStream os = new FileOutputStream(fileName);
- //寫資料
- while ((len = is.read(bs)) != -1) {
- os.write(bs, 0, len);
- }
- //完成後關閉流
- Log.e(TAG, "download-finish");
- os.close();
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- String dirName = Environment.getExternalStorageDirectory() + "/MyDownLoad/";
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Android根據URL下載檔案儲存到SD卡