編輯:關於Android編程
一、自己封裝URLConnection 連接請求類
public void downloadFile1() {
try{
//下載路徑,如果路徑無效了,可換成你的下載路徑
String url = "http://c.qijingonline.com/test.mkv";
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
final long startTime = System.currentTimeMillis();
Log.i("DOWNLOAD","startTime="+startTime);
//下載函數
String filename=url.substring(url.lastIndexOf("/") + 1);
//獲取文件名
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int fileSize = conn.getContentLength();//根據響應獲取文件大小
if (fileSize <= 0) throw new RuntimeException("無法獲知文件大小 ");
if (is == null) throw new RuntimeException("stream is null");
File file1 = new File(path);
if(!file1.exists()){
file1.mkdirs();
}
//把數據存入路徑+文件名
FileOutputStream fos = new FileOutputStream(path+"/"+filename);
byte buf[] = new byte[1024];
int downLoadFileSize = 0;
do{
//循環讀取
int numread = is.read(buf);
if (numread == -1)
{
break;
}
fos.write(buf, 0, numread);
downLoadFileSize += numread;
//更新進度條
} while (true);
Log.i("DOWNLOAD","download success");
Log.i("DOWNLOAD","totalTime="+ (System.currentTimeMillis() - startTime));
is.close();
} catch (Exception ex) {
Log.e("DOWNLOAD", "error: " + ex.getMessage(), ex);
}
}
這種方式在Android 剛興起的時候,很少下載封裝框架,就自己封裝了。雖然一般的文件都能下載,但這種方式缺點很多,不穩定或者各種各樣的問題會出現。
二、Android自定的下載管理(會在notification 顯示下載的進度,同時可以暫停、重新連接等)
private void downloadFile2(){
//下載路徑,如果路徑無效了,可換成你的下載路徑
String url = "http://c.qijingonline.com/test.mkv";
//創建下載任務,downloadUrl就是下載鏈接
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//指定下載路徑和下載文件名
request.setDestinationInExternalPublicDir("", url.substring(url.lastIndexOf("/") + 1));
//獲取下載管理器
DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//將下載任務加入下載隊列,否則不會進行下載
downloadManager.enqueue(request);
}
這種方式其實就是交給了Android系統的另一個app去下載管理。這樣的好處不會消耗該APP的 CPU資源。缺點是:控制起來很不靈活。
三、使用第三方 okhttp 網絡請求框架
private void downloadFile3(){
//下載路徑,如果路徑無效了,可換成你的下載路徑
final String url = "http://c.qijingonline.com/test.mkv";
final long startTime = System.currentTimeMillis();
Log.i("DOWNLOAD","startTime="+startTime);
Request request = new Request.Builder().url(url).build();
new OkHttpClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 下載失敗
e.printStackTrace();
Log.i("DOWNLOAD","download failed");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Sink sink = null;
BufferedSink bufferedSink = null;
try {
String mSDCardPath= Environment.getExternalStorageDirectory().getAbsolutePath();
File dest = new File(mSDCardPath, url.substring(url.lastIndexOf("/") + 1));
sink = Okio.sink(dest);
bufferedSink = Okio.buffer(sink);
bufferedSink.writeAll(response.body().source());
bufferedSink.close();
Log.i("DOWNLOAD","download success");
Log.i("DOWNLOAD","totalTime="+ (System.currentTimeMillis() - startTime));
} catch (Exception e) {
e.printStackTrace();
Log.i("DOWNLOAD","download failed");
} finally {
if(bufferedSink != null){
bufferedSink.close();
}
}
}
});
}
okhttp是一個很有名氣的開源框架,目前已經很多大公司都直接使用它作為網絡請求庫(七牛雲SDK, 阿裡雲SDK)。 且裡面集成了很多優勢,包括 okio (一個I/O框架,優化內存與CPU)。
以上所述是小編給大家介紹的Android 文件下載三種基本方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
Android Context 到底是什麼?
什麼是Context?一個Context意味著一個場景,一個場景就是我們和軟件進行交互的一個過程。比如當你使用微信的時候,場景包括聊天界面、通訊錄、朋友圈,以及背後的一些
解決用android Studio開發不能去除標題欄的問題
之前在eclipse上進行開發的時候,要去除Activity的的默認標題欄,我習慣使用的方法是用下面這行代碼:requestWindowFeature(Window.FE
Android - 五子連珠
繪制棋盤面板:MainActivity.javapackage com.xbmu.wuziqi;import android.support.v7.app.AppComp
Android自定義View—滑動開關
MainActivity如下:+currentStatus, Toast.LENGTH_SHORT).show(); } } } Sw