3

My app requires adding of hundreds markers (in some cases). For this I am using "for loop" which seems to be not bad solution if I am using default markers. I need to use custom image for each marker which I am downloading from the internet with the helping of AsynkTask class Loader(). But when I am using custom markers the UI freezes until "for loop" will be finished.

Adding markers:

private List<User> mUsers = new ArrayList<User>();
private GoogleMap mMap;
Bitmap bAvatar;

.............

private void drawMarkers() {
        mMap.clear();
        for (User user : mUsers) {
            if (user.getGeohash() != null) {
                LatLng pos = gh.decode(user.getGeohash());
                if (user.getAvatar() != null) {
                    try {
                        bAvatar = new Loader().execute(user.getAvatar()).get();                     
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (ExecutionException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    mMap.addMarker(new MarkerOptions()
                            .position(pos)
                            .title(user.getStatus())
                            .icon(BitmapDescriptorFactory.fromBitmap(bAvatar)));
                } else {
                    mMap.addMarker(new MarkerOptions()
                            .position(pos)
                            .title(user.getStatus())
                            .icon(BitmapDescriptorFactory
                                    .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                }

            }
        }
    }

AsyncTask for image downloading:

public class Loader extends AsyncTask<String, Void, Bitmap> {
    Bitmap bitmap;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;
            options.inInputShareable = true;
            mIcon11 = BitmapFactory.decodeStream(in, null, options);
        } catch (Exception e) {
            String error = (e.getMessage() == null) ? "doInBackground - Loader"
                    : e.getMessage();
            Log.e("Error", error);
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

        bitmap = result;

    }
}

I assume that I need to replace "for loop" with the AsynkTask where each user from List<User> will be downloaded asynchronously.

  1. Please help me to create AsynkTask for List<User> array.
  2. Should I combine AsynkTask for List<User> with AsynkTask for image downloading or is it correct to use AsynkTask inside AsynkTask ?

Any help will be appreciated. Thanks in advance.

Juniper
  • 712
  • 1
  • 12
  • 23

1 Answers1

0

First:

  • You shouldn't used nested AsyncTasks. That's not supported.
  • You should however try to combine your image downloads for your users.

Here's what you need to do:

  • Create an asyntask that loops through your users in the doInBackground method.
  • In the doInBackground for each user, download the bitmap,
  • Use the publishProgress method when you've retrieved the bitmap.
  • Calling publishProgress will trigger the onProgressUpdate method on the UI thread, where you can add your marker to the map.

See the DownloadFilesTask example on the Android developers site.

ddewaele
  • 22,363
  • 10
  • 69
  • 82