2
我有各種文件需要從網上下載到手機安卓下載大文件
我也做了代碼,但是代碼只能讓我與小尺寸下載如圖片/照片。一個像.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();
什麼是您看到的故障錯誤?如果是超時問題,HttpUrlConnection允許您設置超時閾值。 – Geoff