我有各种文件需要从网上下载到手机中
我已经完成了代码,但代码只能让我下载图片/照片等小尺寸。像 .apk 这样花费大约 2mb 到 >100mb 的大文件会让我失望。下面是代码:
final ProgressDialog progress=ProgressDialog.show(this, "Please wait", "Loading ...", true);
new Thread()
{
public void run()
{
try {
URL url = new URL("http://www.domain.com/apk/Gmail.apk");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot, "Gmail.apk");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
//downloadedSize += bufferLength;
}
fileOutput.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
progress.dismiss();
}
}.start();