Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying an Image Downloaded from the Internet as Annotation - Using Picasso

I cannot display the image downloaded from the Internet as annotation. I am implementing the following code and Picasso library. However, if I use a local image, it works. Thanks in advance for any help.

private void createAnnotation(int id, double lat, double lon, String caption, String photoUrl) {

    SKAnnotation annotation = new SKAnnotation(id);

    SKCoordinate coordinate = new SKCoordinate(lat, lon);
    annotation.setLocation(coordinate);
    annotation.setMininumZoomLevel(5);

    SKAnnotationView annotationView = new SKAnnotationView();
    View customView =
            (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.annotation_photo_and_text, null, false);
    //  If width and height of the view  are not power of 2 the actual size of the image will be the next power of 2 of max(width,height).
    //annotationView.setView(findViewById(R.id.customView));

    TextView tvCaption = (TextView) customView.findViewById(R.id.annotation_photo_caption);
    tvCaption.setText(caption);

    ImageView ivPhoto = (ImageView) customView.findViewById(R.id.annotation_photo);
    Picasso.with(getApplicationContext())
            .load(photoUrl)
            .resize(96, 96)
            //.centerCrop()
            .into(ivPhoto);
    //ivPhoto.setImageResource(R.drawable.hurricanerain);

    annotationView.setView(customView);
    annotation.setAnnotationView(annotationView);

    mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_NONE);
}
like image 305
burakk Avatar asked Jan 22 '17 14:01

burakk


People also ask

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.


2 Answers

Picasso loads images from the internet asynchronously. Try adding the image to the Annotation after it has been downloaded. You can use a Target to listen for the image download completion:

ImageView ivPhoto = (ImageView) customView.findViewById(R.id.annotation_photo);  
Target target = new Target() {  
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        ivPhoto.setImageBitmap(bitmap);
        annotationView.setView(customView);
        annotation.setAnnotationView(annotationView);
        mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_NONE);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
ivPhoto.setTag(target);
Picasso.with(getApplicationContext())
    .load(photoUrl)
    .resize(96, 96)
    .into(target);
like image 179
Rosário Pereira Fernandes Avatar answered Oct 17 '22 15:10

Rosário Pereira Fernandes


try activity context in place of applicationcontext. It may work for you.

like image 29
user320676 Avatar answered Oct 17 '22 17:10

user320676