2011-03-12 5 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 사용자가 UI 스레드로 진행 게시하여 UI를 변경 [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

오류 평가에서 올바른 내용입니다. textActivity에 정의 된 TextView 개체이며 UI 스레드에서 생성되는 것으로 가정합니다. doInBackground() 내에서 실행되는 코드는 별도의 스레드에서 실행됩니다. UI 스레드 만 UI 요소에 대한 업데이트를 수행 할 수 있으므로 setText 호출하려고하면보고 한 오류 메시지가 나타납니다. publishProgress, onProgressUpdate()를 호출 AsyncTask 당신이 UI 스레드의 배경 스레드에서 업데이트를 보낼 호출 할 수있는 방법이 있기 때문에

Abhinav는 또한 문제를 해결하는 방법에 맞습니다.

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

당신이 무엇을 나에게 설명 할 수있는 "URL는, 정수, 롱"이 "개인적으로 의미 클래스 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'입니다. 다른 문제를 진단하려면 코드를 더 많이 볼 필요가 있습니다. –

관련 문제