2

Hello I want to use async task and this is my sceleton code:

public class MyActivity extends Activity {
    private ProgressDialog pd = null;
    private Object data = null;

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

        // Show the ProgressDialog on this thread
        this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);

        // Start a new thread that will download all the data
        new DownloadTask().execute("Any parameters my download task needs here");
    }

    private class DownloadTask extends AsyncTask<String, Void, Object> {
         protected Object doInBackground(String... args) {
             Log.i("MyApp", "Background thread starting");

             // This is where you would do all the work of downloading your data

             return "replace this with your data object";
         }

         protected void onPostExecute(Object result) {
             // Pass the result data back to the main activity
             MyActivity.this.data = result;

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

My question is what should be the third argument in the AsyncTask if I my do InBackground function return both a string array and a bitmap? Any help?

qwerty_gr
  • 318
  • 3
  • 7
  • 15

2 Answers2

4

You could use a Bundle (see http://developer.android.com/reference/android/os/Bundle.html). So your AsyncTask would look something like...

public class MyTask extends AsyncTask<Void, Void, Bundle> 
{
  @Override
  protected Bundle doInBackground(Void... arg) 
  {
    Bundle bundle = new Bundle();
    bundle.putParcelable("Bitmap", bitmap);
    bundle.putString("String", string);

    return bundle;
  }
  protected void onPostExecute(Bundle bundle) 
  {
    Bitmap bitmap = bundle.getParcelable("Bitmap");
    String string = bundle.getString("String");
  }
}

Bitmap is Parcelable but you may well run into run-time errors for all but the smallest bitmaps. So you might need to turn it into a byte array and back again, something like this...

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray("Bytes", bytes);

and this...

byte[] bytes = bundle.getByteArray("Bytes");
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, new BitmapFactory.Options());
Torid
  • 4,176
  • 1
  • 28
  • 29
2

I don't think there's support for returning two things from an AsyncTask. You could use a simple inner class to hold your return data:

public class MyActivity ... {
   class DownloadTaskResult {
       String stringResult;
       Bitmap bitmapResult;
   }

   class DownloadTask ... extends AsyncTask<String, Void, DownloadTaskResult> {
       protected DownloadTaskResult doInBackground(String... args){
           //your work; return a DownloadTaskResult with the String and Bitmap you want
       }
   }
}
Fishbreath
  • 407
  • 2
  • 9