2017-01-22 26 views
10

Tôi không thể hiển thị hình ảnh được tải xuống từ Internet dưới dạng chú thích. Tôi đang triển khai mã sau đây và thư viện Picasso. Tuy nhiên, nếu tôi sử dụng một hình ảnh địa phương, nó hoạt động. Cảm ơn trước sự giúp đỡ nào.Hiển thị hình ảnh được tải xuống từ Internet dưới dạng chú thích - Sử dụng Picasso

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); 
} 
+0

Không ai làm việc với chú thích có hình ảnh tải xuống từ URL không? – burakk

Trả lời

0

gì nếu bạn cố gắng để tải hình ảnh sử dụng Target đối tượng, và sau đó thiết lập bitmap tải về ImageView của bạn?

Target target = new Target() { 
    @Override 
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
     // loading of the bitmap was a success 
     // TODO do some action with the bitmap 
     ivPhoto.setImageBitmap(bitmap); 
    } 

    @Override 
    public void onBitmapFailed(Drawable errorDrawable) { 
     // loading of the bitmap failed 
     // TODO do some action/warning/error message 
    } 

    @Override 
    public void onPrepareLoad(Drawable placeHolderDrawable) { 

    } 
}; 
ivPhoto.setTag(target); 
Picasso.with(getApplicationContext()) 
    .load(photoUrl) 
    .resize(96, 96) 
    .into(target); 
1

Picasso tải hình ảnh từ internet một cách không đồng bộ. Thử thêm hình ảnh vào Chú thích sau khi nó đã được tải xuống. Bạn có thể sử dụng Mục tiêu để nghe để hoàn tất tải xuống hình ảnh:

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); 
+0

Tôi đã giải quyết được vấn đề bằng cách sử dụng ImageRequest (trong Volley). Tôi đã không kiểm tra mã của bạn, nhưng có vẻ như nó sẽ hoạt động. Cảm ơn bạn. – burakk

+0

Sao chép một câu trả lời khác không phải là một cách tiếp cận tốt, người đàn ông. – rom4ek

1

thử ngữ cảnh hoạt động thay cho bối cảnh ứng dụng. Nó có thể làm việc cho bạn.

Các vấn đề liên quan