2014-05-10 212 views
1

当我尝试下载.zip文件。我没有得到完整的文件,文件大小为3.93 MB,但我只得到38KB.Don't得到完整的.zip文件 这里是我的代码AsyncTask下载zip文件android

  1. 活动课

    private Button btnStart; 
         // Progress Dialog Object 
         private ProgressDialog prgDialog; 
         // Progress Dialog type (0 - for Horizontal progress bar) 
         public static final int progress_bar_type = 0; 
         // Music resource URL 
         private static String file_url = "http://www.mediafire.com/download/tvd070dounxk472/al_Quran_ul_Hakeem.zip"; 
    @Override 
         public void onCreate(Bundle savedInstanceState) { 
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.main); 
          btnStart = (Button) findViewById(R.id.btnProgressBar); 
          btnStart.setOnClickListener(new View.OnClickListener() { 
           public void onClick(View v) { 
            btnStart.setEnabled(false); 
            File file = new File(Environment.getExternalStorageDirectory().getPath()+"/al_Quran_ul_Hakeem.zip"); 
            // Check if the Music file already exists 
            if (file.exists()) { 
             Toast.makeText(getApplicationContext(), "File already exist under SD card", Toast.LENGTH_LONG).show(); 
    

    }

    否则{

    new DownloadMusicfromInternet().execute(file_url); 
    } 
    

    } }); }

  2. 的AsyncTask类

    类DownloadMusicfromInternet扩展的AsyncTask {

     @Override 
         protected void onPreExecute() { 
          super.onPreExecute(); 
          // Shows Progress Bar Dialog and then call doInBackground method 
          showDialog(progress_bar_type); 
         } 
    
         @Override 
         protected String doInBackground(String... f_url) { 
          int count; 
          try { 
           URL url = new URL(f_url[0]); 
           URLConnection conection = url.openConnection(); 
           conection.connect(); 
    
           int lenghtOfFile = conection.getContentLength(); 
           // input stream to read file - with 8k buffer 
           InputStream input = new BufferedInputStream(url.openStream(),100*1024); 
           // Output stream to write file in SD card 
           OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/al_Quran_ul_Hakeem.zip"); 
           byte data[] = new byte[1024]; 
           long total = 0; 
           while ((count = input.read(data)) != -1) { 
            total += count; 
            // Publish the progress which triggers onProgressUpdate method 
            publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 
    
            // Write data to file 
            output.write(data, 0, count); 
           } 
           // Flush output 
           output.flush(); 
           // Close streams 
           output.close(); 
           input.close(); 
          } catch (Exception e) { 
           Log.e("Error: ", e.getMessage()); 
          } 
          return null; 
         } 
    
         // While Downloading Music File 
         protected void onProgressUpdate(String... progress) { 
          // Set progress percentage 
          prgDialog.setProgress(Integer.parseInt(progress[0])); 
         } 
    
         // Once Music File is downloaded 
         @Override 
         protected void onPostExecute(String file_url) { 
    
          dismissDialog(progress_bar_type); 
          Toast.makeText(getApplicationContext(), "Download complete", Toast.LENGTH_LONG).show(); 
         } 
        } 
    
+0

你从'得到什么价值conection.getContentLength()'? –

+0

我正在获取文件的长度来更新进度条 – user3582794

+0

这似乎没问题,也许链接是问题。顺便说一下,不要忘记关闭finally块上的流,目前的做法是不好的做法。 –

回答

-1

你检查你真的可以下载该文件?如果我打开该链接,我将重定向到一个网页,而不是下载本身。

因此,检查你得到的不是真的HTML页面...如果是这样的话,你将需要一个直接的下载链接。

编辑:BTW该文件的直接下载链接是http://download2010.mediafire.com/ugkwnc164pqg/tvd070dounxk472/al_Quran_ul_Hakeem.zip,但我不知道这是否是不时固定链接或变更...

+0

链接不时变化..我怎么能得到固定链接,并直接下载文件不到HTML页面 – user3582794

+0

,因为我正在使用的链接...直接下载文件,但不是它打开html页面 – user3582794

+0

因此,使用直接链接的作品?关于是否修复,如果你想让链接保持不变,你可以问MediaFire,如果他们提供这样的功能,使用一个不同的服务,可以给你(例如:公共文件夹链接的DropBox),或者你需要得到首先是HTML页面,然后解析它以找到那里的直接链接......当然,第一个/第二个选项会容易得多。 – Salem