2010-06-30 40 views
2

以下代碼執行一個函數,該函數通過ftp檢索文件,然後將其顯示在imageview中。由於我使用的主線程的UI鎖定,有人告訴我,我可以使用asynctask來做這個工作,但我不明白:<創建多個線程以實現ftp下載並在imageview中顯示

是否有人熟悉這可以給我一些指導?

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

您好布魯克斯爲了避免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 2010-06-30 23:13:50

相關問題