Android的下载Zip到SD卡? [英] Android Download Zip to SD card?

查看:99
本文介绍了Android的下载Zip到SD卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要的应用程序首次启动时,下载一个100兆的zip文件。它需要解压到SD卡(400兆)。

I have a 100 meg zip file that I need to download when the app first starts. It needs to unzip to the SD card (400 meg).

我宁愿不必须有它触摸手机的存储尽可能多的手机将不会有400兆的免费手机的存储空间。

I'd rather not have to have it touch the phone's storage as many phones won't have 400 meg free on the phone's storage.

可以这样做(任何一个有一个例子吗?)

Can this be done (any one have an example?)

谢谢, 伊恩

推荐答案

可以做到的。你到底在找什么?下载的程序或如何做检查? 这里的下载方法,你应该运行在AsyncTask的左右。

Can be done. What exactly are you looking for? The download routine or how to do the check? Here's the download method, you should run it in an AsyncTask or so.

/**
 * Downloads a remote file and stores it locally
 * @param from Remote URL of the file to download
 * @param to Local path where to store the file
 * @throws Exception Read/write exception
 */
static private void downloadFile(String from, String to) throws Exception {
    HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
    conn.setDoInput(true);
    conn.setConnectTimeout(10000); // timeout 10 secs
    conn.connect();
    InputStream input = conn.getInputStream();
    FileOutputStream fOut = new FileOutputStream(to);
    int byteCount = 0;
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = input.read(buffer)) != -1) {
        fOut.write(buffer, 0, bytesRead);
        byteCount += bytesRead;
    }
    fOut.flush();
    fOut.close();
}

您还可能要检查手机是否被至少连接到无线网络(和3G);

You also might want to check whether the phone is at least connected to WiFi (and 3G);

// check for wifi or 3g
ConnectivityManager mgrConn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if ((mgrConn.getActiveNetworkInfo()!=null && mgrConn.getActiveNetworkInfo().getState()==NetworkInfo.State.CONNECTED)
       || mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) { 
 ...

否则人们会很生气时,他们需要通过一个缓慢的手机网络下载100微米。

otherwise people will get mad when they need to download 100m via a slow phone network.

这篇关于Android的下载Zip到SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆