3

I'm trying to do something like this: Android Map api v2 Custom marker with ImageView But I'm stuck on using image loader.

Trying:

Bitmap bmImg = imageLoader.loadImageSync(url);

LogCat gives me

 04-13 14:11:44.953: E/ImageLoader(18542): android.os.NetworkOnMainThreadException

Here's is my code. I have an ArrayList camera with all information needed (titrle, position, url, etc).

public void drawPicsOnMap()
{                       
    String title = null;
    String place = null;        
    for (int i = 0; i<camera.size(); i++)
    {
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
        Canvas canvas = new Canvas(bmp);

        // paint defines the text color,
        // stroke width, size
        Paint color = new Paint();
        color.setTextSize(35);
        color.setColor(Color.BLACK);

        Camera cam = camera.get(i);
        LatLng coord = new LatLng(cam.lat, cam.lon);

        title = Integer.toString(cam.id);
        place = cam.place;                                  

        String url = cam.img;
        Bitmap bmImg = imageLoader.loadImageSync(url);

        //modify canvas
        canvas.drawBitmap(bmImg, 0,0, color);
        canvas.drawText(title, 30, 40, color);

        Marker mark = map.addMarker(new MarkerOptions()
        .position(coord)
        .title(title)
        .snippet(place)
        .icon(BitmapDescriptorFactory
                .fromBitmap(bmp)));

        markers.put(mark.getId(), url);
    }
}
Community
  • 1
  • 1
Paul Chernenko
  • 696
  • 5
  • 21

2 Answers2

6

Try this:

imageLoader.loadImage(url, new SimpleImageLoadingListener(){

                    @Override
                    public void onLoadingComplete(String imageUri, View view,
                            Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);

                        //write your code here to use loadedImage
                    }

                });

Here onLoadingComplete will be called on UI thread which makes it thread safe

Shahidul
  • 2,997
  • 1
  • 21
  • 20
  • I made Bitmap bmImg = null; and called imageLoader.loadImage() what you have writed. Inside onLoadingComplete i've written bmImg = loadedImage. Now LogCat tells me java.lang.NullPointerException – Paul Chernenko Apr 13 '14 at 13:51
  • You are using bmImg before onLoadingComplete function is called. – Shahidul Apr 13 '14 at 15:28
  • Remember onLoadingComplete is called whenever image download is completed. Since it will take a few time to download the image. So you can't get and use Bitmap before onLoadingComplete is called. – Shahidul Apr 13 '14 at 15:34
  • You were right, I moved addMarker to onLoadingComplete, everything works! Thanks :) – Paul Chernenko Apr 13 '14 at 16:41
0

You can not do any networking on the main thread. See for description. UIL usually creates the thread and stuff for you when you call displayImage, but loadImageSync does not. Either create a thread and load it or use an AsynTask.

KennyC
  • 593
  • 4
  • 12
  • There is UIL.loadImage(String uri, ImageLoadingListener listener) that doesn't work on main thread. But I can't get bitmap from there :( – Paul Chernenko Apr 13 '14 at 12:09
  • Do something like this: Create an async task and load the bitmap in doInBackground, pass the bitmap to the onPostExecute method then set the marker with that bitmap. – KennyC Apr 13 '14 at 12:11