2011-07-12 80 views
0

我試圖下載使用AsyncTask到它正常工作模擬器回地面MP文件..但在divice它不顯示progressbar 這個問題是由於當我運行此代碼的Android的AsyncTask是-1需要幫助

int lenghtOfFile = c.getContentLength(); 
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

這給了我-1爲什麼這個幸福?我的代碼是

@Override 
      protected String doInBackground(String... arg0) { 
       // TODO Auto-generated method stub 
       // mp3load(); 
       int len1 = 0; 
       int count; 
       try { 
        URL url = new URL(GlobalVariable.Getstr()); 
        HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
        c.setRequestMethod("GET"); 
        c.setDoOutput(true); 
        c.connect(); 
        int lenghtOfFile = c.getContentLength(); 
        Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
        String PATH = Environment.getExternalStorageDirectory() 
          + "/download/"; 
        Log.v(LOG_TAG, "PATH: " + PATH); 
        File file = new File(PATH); 
        file.mkdirs(); 

        // String fileName = "workTest.mp3"; 
        String fileName = GlobalVariable.Getstrpath().toString(); 

        File outputFile = new File(file, fileName); 

        FileOutputStream fos = new FileOutputStream(outputFile); 

        InputStream is = c.getInputStream(); 

        byte[] buffer = new byte[1024]; 
        long total = 0; 

        while ((count = is.read(buffer)) != -1) { 
         total += count; 
         publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 
         // fos.write(buffer, 0, len1); 
         fos.write(buffer, 0, count); 
        } 
        fos.close(); 
        is.close(); 

       } catch (IOException e) { 
        Log.d(LOG_TAG, "Error: " + e); 
       } 
       return null; 
      } 

      protected void onProgressUpdate(String... progress) { 
       Log.d("ANDRO_ASYNC", progress[0]); 

       mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
      } 

      protected void onPostExecute(String unused) { 
       dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
       refreshList(); 
       Toast.makeText(
         FindFilesByType.this, 
         "Downloading of " + GlobalVariable.Getstrpath() 
           + " complete.", Toast.LENGTH_LONG).show(); 
    //   Intent i = new Intent(FindFilesByType.this, SongList.class); 
    //   finish(); 
    //   startActivity(i); 
       try{ 
       Intent i = new Intent(FindFilesByType.this, SongList.class); 
       finish(); 
       startActivity(i);}catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 
     } 

的entiere代碼工作正常,模擬器
但它總是不顯示我的進步,但下載發生成功地
感謝Pragna

回答

0

Somethimes的Java將無法獲取遠程的大小文件,我認爲這是你的問題。 但是在調試代碼時請檢查手機上的網絡連接。

+0

但它總是不顯示我的進展,但下載成功發生 – Android

0

得到了同樣的問題(在Android 4.0只),以及移除

c.setDoOutput(true); 

固定對我來說。