2010-06-30 4 views
2

다음 코드는 ftp를 통해 파일을 검색 한 다음이를 이미지 뷰에 표시하는 함수를 실행합니다. UI가 잠겨있는 주 스레드를 사용하고 있기 때문에 누군가가 asynctask를 사용하여이 작업을 수행 할 수 있다고 말합니다. 그러나 이것을 알아낼 수는 없습니다. <ftp 다운로드 및 이미지 뷰로 표시하기 위해 여러 스레드 만들기

아무 지침이 있습니까?

package hhh.ggg; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.os.Bundle; 
import android.os.Handler; 

import java.io.File; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.widget.TextView; 
//import it.sauronsoftware.ftp4j.FTPClient; 

import it.sauronsoftware.ftp4j.*; 


public class hhh extends Activity { 
    /** Called when the activity is first created. */ 
FTPClient ftp; 
String host = "ipofFTPserver"; 

    final Handler mHandler = new Handler(); 
    final Runnable mUpdateResults = new Runnable() { 
     public void run() { 
      updateResultsInUi(); 
     } 
    }; 



@Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     startLongRunningOperation(); 

} 

protected void startLongRunningOperation() { 

     // Fire off a thread to do some work that we shouldn't do directly in the UI thread 
     Thread t = new Thread() { 
      public void run() { 

      try 
      { 
      ftp = new FTPClient(); 
      ftp.connect(host); 
      ftp.login("username", "password"); 
      ftp.changeDirectory("public_ftp"); 
     ftp.download("test.jpg", new java.io.File("/sdcard/test.jpg")); 
      } 
     catch(Exception e) 
     {     

     } 



       mHandler.post(mUpdateResults); 
      } 
     }; 
     t.start(); 
    } 

private void updateResultsInUi() { 

     // Back in the UI thread -- update our UI elements based on the data in mResults 


    TextView jpgName = (TextView)findViewById(R.id.jpgname);  
    ImageView jpgView = (ImageView)findViewById(R.id.jpgview); 
    String myJpgPath = "/sdcard/test.jpg";    
     jpgName.setText(myJpgPath); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 2; 
    Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options); 
    jpgView.setImageBitmap(bm); 
    } 




} 

답변

1

안녕하세요 brux가 UI 잠금을 피하려면 AsyncTask를 사용하고 다음과 같이해야합니다.

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main); 

    this.pd = ProgressDialog.show(this, "Working...", "*** Downloading Data...", true, false); 
    new DownloadTask().execute(" parameters needed for download"); 
} 

private class DownloadTask extends AsyncTask<String, Void, Object> { 
    protected Void doInBackground(String... args) { 
     Log.d("********Jorgesys", "Background thread starting......"); 
     startLongRunningOperation(); 
     return null;   
    } 

    protected void onPostExecute(Object result) { 
     Log.d("********Jorgesys", "onPostExecute......"); 

     if (Splash.this.pd != null) { 
      Splash.this.pd.dismiss(); 
     } 
     updateResultsInUi(); 

    } 
} 


protected void startLongRunningOperation() { 
      try 
      { 
      ftp = new FTPClient(); 
      ftp.connect(host); 
      ftp.login("username", "password"); 
      ftp.changeDirectory("public_ftp"); 
     ftp.download("test.jpg", new java.io.File("/sdcard/test.jpg")); 
      } 
     catch(Exception e) 
     {     

     } 
} 
+0

우수 jorgesys – brux

관련 문제