tags:

views:

82

answers:

1

The following code executes a function that retrieves a file via ftp and then displays it in an imageview. Since I'm using the main thread the UI locks up, somebody tells me I can use asynctask to make this work but I can't figure it out :<

Is anybody familiar with this that could offer me some guidance?

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  A: 

Hi brux to avoid UI locks up use AsyncTask, you must implement somethin like this.

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)
      {                

      }
}
Jorgesys
Excellent thank you jorgesys
brux