Upload image from a url to an InfoWindowAdapter

0

I'm trying to display an image from a URL in an "InfoWindowAdapter", but it's never shown. I'm using the Volley library to upload images.

Does anyone have an idea how to solve this problem?

//no fragment mapa
 map.setInfoWindowAdapter(adapter);

//o adapter
public class InfoViewDetailAdvertiser implements InfoWindowAdapter {

private final View myContentsView;

private ArrayList<Advertiser> advertisers;
private ImageLoader mImageLoader;
private RequestQueue queue;

private NetworkImageView localImage;
private TextView localName;
private TextView localAddressStreetNumber;
private TextView localAddressDistrict;
private TextView localAddressCityState;
private TextView localPhoneNumber;

public InfoViewDetailAdvertiser(Activity activity, ArrayList<Advertiser> advertisers) {
    this.advertisers = advertisers;
    myContentsView = activity.getLayoutInflater().inflate(R.layout.adapter_local, null);

    queue = RequestSingleton.getInstance(
            activity.getApplicationContext()).getRequestQueue();

    mImageLoader = new ImageLoader(this.queue, new LruBitmapCache(
            LruBitmapCache.getCacheSize(activity.getApplicationContext())));
}

public Advertiser getAdvertiser(String id) {
    for (Advertiser advertiser : advertisers) {
        if (id.equals(String.valueOf(advertiser.getId()))) {
            return advertiser;
        }
    }
    return null;
}

@Override
public View getInfoContents(Marker marker) {
    mapView();
    Advertiser advertiser = getAdvertiser(marker.getTitle());
    loadData(advertiser);

    return myContentsView;
}

private void loadData(Advertiser advertiser) {
    if (advertiser != null) {
        localImage.setImageUrl(advertiser.getLogo(), mImageLoader);
        localName.setText(advertiser.getName());
        loadAdress(advertiser);
        loadPhone(advertiser);
    }
}

private void loadPhone(Advertiser advertiser) {
    localPhoneNumber.setVisibility(View.VISIBLE);
    if (advertiser.getPhones() != null && advertiser.getPhones().size() > 0) {
        localPhoneNumber.setText(advertiser.getPhones().get(0));
    } else {
        localPhoneNumber.setVisibility(View.GONE);
    }
}

private void loadAdress(Advertiser advertiser) {
    if (advertiser.getAddress() != null) {
        String street = advertiser.getAddress().getStreet();
        String number = advertiser.getAddress().getNumber();
        localAddressStreetNumber.setVisibility(View.VISIBLE);
        if (street != null && number != null) {
            localAddressStreetNumber.setText(street + ", " + number);
        } else if (street != null) {
            localAddressStreetNumber.setText(street);
        } else {
            localAddressStreetNumber.setVisibility(View.GONE);
        }

        localAddressDistrict.setVisibility(View.VISIBLE);
        if (advertiser.getAddress().getDistrict() != null) {
            localAddressDistrict.setText(advertiser.getAddress().getDistrict());
        } else {
            localAddressDistrict.setVisibility(View.GONE);
        }

        String city = advertiser.getAddress().getCity();
        String state = advertiser.getAddress().getState();

        localAddressCityState.setVisibility(View.VISIBLE);
        if (city != null && state != null) {
            localAddressCityState.setText(city + " - " + state);
        } else if (city != null) {
            localAddressCityState.setText(city);
        } else if (state != null) {
            localAddressCityState.setText(state);
        } else {
            localAddressCityState.setVisibility(View.GONE);
        }
    }
}

private void mapView() {
    localImage = ((NetworkImageView) myContentsView.findViewById(R.id.item_imagem_local));
    localName = ((TextView) myContentsView.findViewById(R.id.item_nome_local));
    localAddressStreetNumber = ((TextView) myContentsView.findViewById(R.id.item_endereco_logradouro_numero));
    localAddressDistrict = ((TextView) myContentsView.findViewById(R.id.item_endereco_bairro));
    localAddressCityState = ((TextView) myContentsView.findViewById(R.id.item_endereco_cidade_estado));
    localPhoneNumber = ((TextView) myContentsView.findViewById(R.id.item_telefone_local));
}

@Override
public View getInfoWindow(Marker marker) {
    return null;
}
}

//classe LruBitmapCache
public class LruBitmapCache extends LruCache<String, Bitmap>
    implements ImageCache {

public LruBitmapCache(int maxSize) {
    super(maxSize);
}

public LruBitmapCache(Context ctx) {
    this(getCacheSize(ctx));
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight();
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
    final DisplayMetrics displayMetrics = ctx.getResources().
            getDisplayMetrics();
    final int screenWidth = displayMetrics.widthPixels;
    final int screenHeight = displayMetrics.heightPixels;
    // 4 bytes per pixel
    final int screenBytes = screenWidth * screenHeight * 4;

    return screenBytes * 3;
}
}

 //classe RequestSingleton
 public class RequestSingleton{

private static RequestSingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private RequestSingleton(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap>
                cache = new LruCache<String, Bitmap>(20);

        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            cache.put(url, bitmap);
        }
    });
}

public static synchronized RequestSingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new RequestSingleton(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}
}
    
asked by anonymous 06.01.2015 / 20:03

1 answer

1

I was able to solve the problem. You had to download the image manually. How was 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();
    }
}
    
07.01.2015 / 13:37