Android 下载 Zip 到 SD 卡?

发布于 2024-09-12 08:55:12 字数 146 浏览 3 评论 0原文

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

我不想让它接触手机的存储空间,因为许多手机的手机存储空间都没有 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).

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?)

Thanks,
ian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-09-19 08:55:12

可以做到。您到底在寻找什么?下载例程或如何进行检查?
这是下载方法,您应该在 AsyncTask 左右运行它。

/**
 * 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();
}

您可能还想检查手机是否至少连接到 WiFi(和 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) { 
 ...

否则当人们需要通过慢速电话网络下载 100m 时,他们会生气。

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();
}

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) { 
 ...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文