Search code examples
androidgoogle-maps-api-3android-volleyimage-load

Add an Image from url into InfoWindowAdapter


I'm trying to display an image from a URL in an InfoWindowAdapter, but it does not show me the image. I'm using Volley to load images.

Does anyone have an idea how to solve this problem?

Thanks for your help!


Solution

  • I got it solve the problem. It was necessary to download the image manually. How did the code:

    private void loadImage(Marker marker) {
    if (((BitmapDrawable) localImage
            .getDrawable()) == null) {
        new DownloadImage(localImage, marker).execute(urlImage);
    }
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    
    private ImageView icone;
    private Marker marker;
    
    public DownloadImage(ImageView imageView, Marker marker) {
        icone = imageView;
        this.marker = marker;
    }
    
    @Override
    protected Bitmap doInBackground(String... URL) {
    
        String imageURL = URL[0];
        Bitmap bitmap = null;
        try {
            // Download Image from URL
            InputStream input = new java.net.URL(imageURL).openStream();
            bitmap = BitmapFactory.decodeStream(input);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    
    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            icone.setImageBitmap(result);
        } else {
            icone.setBackgroundResource(R.drawable.ic_launcher);
        }
        marker.showInfoWindow();
    }
    

    }