2011-03-12 26 views
1
private class DownloadLargeFileTask extends AsyncTask<Void, Void, Void> 
{ 
    private final ProgressDialog dialog; 

    public DownloadLargeFileTask(ProgressDialog dialog) { 
     this.dialog = dialog; 
    } 

    protected void onPreExecute() { 
     dialog.show(); 
    } 


    protected void onPostExecute(Void unused) { 
     dialog.dismiss(); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     download(); 
     return null; 
    } 
} 

private void download() 
{ 
    try 
    { 
     long startTime = System.currentTimeMillis(); 

     URL u = new URL("http://mds.podfm.ru/188/download/040_Sergejj_Palijj_-_Karantin_CHast_2.mp3"); 
     HttpURLConnection c = (HttpURLConnection) u.openConnection(); 

     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 
     FileOutputStream f = new FileOutputStream(new File("/sdcard/","my.mp3")); 

     InputStream in = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     int downloadedSize = 0; 
     while ((len1 = in.read(buffer)) != -1) 
     { 
      f.write(buffer,0, len1); 
      downloadedSize += len1; 
      ReturnDownloadedBytes(downloadedSize); 
     } 
     f.close(); 
     Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
    } 
    catch (IOException e) 
    { 
     Log.d("ImageManager", "Error" + ((System.currentTimeMillis())/1000) + e + " sec"); 
    } 
} 

private void ReturnDownloadedBytes(int size) 
{ 
    text.setText(String.valueOf(size)); 

} 

錯誤說:只有創建視圖層次結構的原始線程可以觸及其視圖。 我認爲這意味着我從一個therad創建textview並嘗試從另一個(AsyncTask)獲取訪問權限,但是如何獲取它? 感謝試圖獲得下載文件的大小,但得到一個錯誤

編輯
這裏是所有代碼。但即使我在publishProgress(downloadedSize) int value = 10發送alwys在text.setText(String.valueOf(progress));不同值輸出像[Ljava.lang.float;@43ed62f78

public class list extends Activity { 

private TextView text; 



@Override 
public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    text = (TextView)findViewById(R.id.result); 
} 

public void selfDestruct(View view) { 

    ProgressDialog dialog = new ProgressDialog(this); 
    dialog.setMessage("????????. ?????..."); 
    new DownloadLargeFileTask(dialog).execute(); 

} 


private class DownloadLargeFileTask extends AsyncTask<Void, Integer, Void> 
{ 
    private final ProgressDialog dialog; 

    public DownloadLargeFileTask(ProgressDialog dialog) { 
     this.dialog = dialog; 
    } 

    protected void onPreExecute() { 
     dialog.show(); 
    } 


    protected void onPostExecute(Void unused) { 
     dialog.dismiss(); 
    } 

    protected void onProgressUpdate(Integer...progress) { 

     text.setText(String.valueOf(progress)); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     try 
     { 
      long startTime = System.currentTimeMillis(); 

      URL u = new URL("http://mds.podfm.ru/188/download/040_Sergejj_Palijj_-_Karantin_CHast_2.mp3"); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(); 

      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 
      FileOutputStream f = new FileOutputStream(new File("/sdcard/","my.mp3")); 

      InputStream in = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      int downloadedSize = 10; 
      while ((len1 = in.read(buffer)) != -1) 
      { 
       f.write(buffer,0, len1); 
       //downloadedSize += len1; 
       **publishProgress(downloadedSize);** 
      } 
      f.close(); 
      Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
     } 
     catch (IOException e) 
     { 
      Log.d("ImageManager", "Error" + ((System.currentTimeMillis())/1000) + e + " sec"); 
     } 
     return null; 
    } 
} 

回答

2

你對錯誤的評估是正確的。我假設text是在Activity中定義的TextView對象,因此它是在UI線程中創建的。在doInBackground()內運行的代碼在單獨的線程中運行。只有UI線程可以執行UI元素的更新,因此當您嘗試撥打setText時,您會收到您報告的錯誤消息。

阿比納夫也是如何解決問題的正確,如AsyncTask有,您可以調用從後臺線程到UI線程發送更新的方法:publishProgress,這就要求onProgressUpdate()

此方法添加到您的AsyncTask

@Override 
protected void onProgressUpdate(Integer... integer){ 
    text.setText(integer[0].toString()); 
} 

,並更改while迴路download()

while ((len1 = in.read(buffer)) != -1) 
{ 
    f.write(buffer,0, len1); 
    downloadedSize += len1; 
    publishProgress(downloadedSize); 
} 
+0

你可以給我解釋一下什麼是 「網址,爲Integer,Long」 是指在這個「私人類DownloadFilesTask擴展AsyncTask 「,以及這個params /的順序有什麼不同。感謝 – SERG

+0

,當我調用text.setText(String.valueOf(progress));結果我在TextView中得到了類似[Ljava.lang.float; @ 43ed62f78 - 這是什麼意思? – SERG

+0

@SERG檢查[documentation](http://developer.android.com/reference/android/os/AsyncTask.html),它們是'Params','Progress'和'Result'。我需要看到更多的代碼來診斷其他問題。 –

相關問題