Upload image from a url to an Infowindowadapter

Asked

Viewed 189 times

0

I am trying to show an image from a URL on an "Infowindowadapter", but it is never shown. I’m using the Volley library to upload images.

Someone has 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;
}
}
  • Can you put how you’re looking for the image with Volley? It’s using an Imageloader or Imagerequest?

  • I see no problem in your code, but it was not clear how from the InfoWindowAdapter you are making the request to Volley. Could add this code snippet?

  • The advertiser is filled in correctly? In the setImageUrl is making a mistake?

  • No error occurs. I have checked that the data is correctly filled, and are.

  • It’s as if Adapter didn’t update after downloading the image

  • Try to use the ImageRequest, placing a breakpoint on callback Just to make sure it’s being loaded. You have an example here: http://developer.android.com/training/volley/request.html#request-image.

  • The Adapter doesn’t even get a response from the requisition. The same code I’m using on this Adapter, use on a Basedapter and the loading goes smoothly.

  • Thanks for the help. I managed to solve the problem. I downloaded it by hand, without the library.

Show 3 more comments

1 answer

1


I was able to solve the problem. It was necessary to download the image manually. How did the code turn out:

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();
    }
}
  • Do not forget to mark as correct, otherwise it will seem that the problem is pending.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.