Android下载Zip到SD卡?
我有一个100兆的zip文件,我需要在应用程序首次启动时下载。它需要解压缩到SD卡(400兆) 我不想让它触碰手机的存储空间,因为很多手机的存储空间里不会有400兆的空闲空间 这能做到吗(有人举个例子吗?) 谢谢,Android下载Zip到SD卡?,android,zip,Android,Zip,我有一个100兆的zip文件,我需要在应用程序首次启动时下载。它需要解压缩到SD卡(400兆) 我不想让它触碰手机的存储空间,因为很多手机的存储空间里不会有400兆的空闲空间 这能做到吗(有人举个例子吗?) 谢谢, 伊恩可以做到。你到底在找什么?下载程序或如何进行检查? 这里是下载方法,您应该在异步任务中运行它 /** * Downloads a remote file and stores it locally * @param from Remote URL of the file to
伊恩可以做到。你到底在找什么?下载程序或如何进行检查? 这里是下载方法,您应该在异步任务中运行它
/**
* 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)
否则,当人们需要通过慢速电话网络下载100m时,他们会发疯
// 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) {
...