Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating multiple threads to achieve ftp download and display in imageview
    text
    copied!<p>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 :&lt;</p> <p>Is anybody familiar with this that could offer me some guidance?</p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload