Java – How to load an image using Universal ImageLoader without display

How to load an image using Universal ImageLoader without display… here is a solution to the problem.

How to load an image using Universal ImageLoader without display

I’m trying to do something like this:
Android Map api v2 Custom marker with ImageView
But I stick with the image loader.

Try:

Bitmap bmImg = imageLoader.loadImageSync(url);

LogCat to me

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

Here is my code. I have an ArrayList camera with all the required information (title, location, 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);
    }
}

Solution

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
                    }

});

Over here.
onLoadingComplete will be called on the UI thread, which makes it thread-safe

Related Problems and Solutions