how to load image with Universal ImageLoader witho

2020-06-06 02:54发布

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);
    }
}

2条回答
家丑人穷心不美
2楼-- · 2020-06-06 03:08

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

查看更多
乱世女痞
3楼-- · 2020-06-06 03:23

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.

查看更多
登录 后发表回答