2012-06-09 109 views
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(); 
+0

什么是您看到的故障错误?如果是超时问题,HttpUrlConnection允许您设置超时阈值。 – Geoff

回答

2

请使用下面的链接对这些代码,它可以帮助你。

Download File

+0

完美!谢谢 :) – melvintcs