如何将图像从url加载到InfoWindowAdapter android?图像未显示

时间:2015-01-24 01:38:23

标签: android google-maps android-asynctask imageview infowindow

我正在尝试在“InfoWindowAdapter”中显示来自网址的图片,我有以下代码,但没有向我展示图片。有什么不对吗?

public class ObjectInfoWindow implements GoogleMap.InfoWindowAdapter {

    private Activity activity;
    private HashMap<String, LostObject> markers;
    private Marker markerShowingInfoWindow;
    private boolean mRefreshingInfoWindow;
    private View v = null;
    ImageUrlView imgThumbnail;

    public LostObjectInfoWindow(Activity activity, HashMap<String, LostObject> markers) {

        this.activity = activity;
        this.markers = markers;

    }

    @Override
    public View getInfoContents(Marker marker) {

        DebugLog.d("TAG", "getInfoContents mRefreshingInfoWindow "+mRefreshingInfoWindow);

        if(v==null){
            v = activity.getLayoutInflater().inflate(R.layout.lost_object_info_window, null);
        }

        LostObject lostObject = markers.get(marker.getId());
        if (lostObject != null) {

            String imgThumbnailPath = lostObject.getPhoto();

            if(imgThumbnailPath==null || imgThumbnailPath.trim().length() == 0){
            TextView title = (TextView) v.findViewById(R.id.title);
            TextView description = (TextView) v.findViewById(R.id.description);
            title.setText(lostObject.getType());

            if (lostObject.getContact() != null) {
                description.setText(activity.getResources().getString(R.string.lost_object_contact_info_window, lostObject.getContact()));
            }
                imgThumbnail = (ImageUrlView) v.findViewById(R.id.thumbnail);
                imgThumbnail.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imgThumbnail.setImageResource(R.drawable.ic_ayn_list_grey);

            } else {


                if (!mRefreshingInfoWindow) {

                    TextView title = (TextView) v.findViewById(R.id.title);
                    TextView description = (TextView) v.findViewById(R.id.description);
                    title.setText(lostObject.getType());

                    if (lostObject.getContact() != null) {
                        description.setText(activity.getResources().getString(R.string.lost_object_contact_info_window, lostObject.getContact()));
                    }

                    imgThumbnail = (ImageUrlView) v.findViewById(R.id.thumbnail);

                    markerShowingInfoWindow = marker;

                    imgThumbnailPath = imgThumbnailPath.replace(".jpg", "_100_100.jpg");
                    imgThumbnail.setListener(listener);
                    imgThumbnail.load(imgThumbnailPath);

                }else{
                    v.invalidate();

                }
            }


        }

        // Returning the view containing InfoWindow contents
        return v;
    }

在加载位图后调用此方法。它检查当前是否显示  信息窗口是已保存的信息窗口。如果是,则刷新窗口以显示新加载的图像。

private ImageUrlView.ImageUrlViewListener listener = new ImageUrlView.ImageUrlViewListener() {

        @Override
        public void imageAdded(ImageUrlView img) {

            if (markerShowingInfoWindow != null ) {
                mRefreshingInfoWindow = true;
                markerShowingInfoWindow.showInfoWindow();
                mRefreshingInfoWindow = false;
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

首先,我没有看到存储URL路径/地址的任何变量。因此,程序无法获取图像的位置并将其显示在信息窗口上。有两种方法可以做到这一点:

  • 一个是使用Universal Image Loader,它是一个可以嵌入到您的应用程序项目中的库。这与使用custominfoadapter在您的应用代码中执行的操作非常相似。有关代码的完整实现,请参阅此link
  • 另一种方法是使用单独的线程(Async Task是最好的选择)。这将从后台的URL加载图像,并以同步方式将其粘贴到custominfowindow中。

这是一个代码片段,将此方法放在ObjectInfoWindow类中,并将URL和标记作为参数传递:

protected void handleMarkerClicked(final Marker marker, final String url) {
        new AsyncTask<Void, Void, Void>()
        {   
            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();
                _infoImageDrawable = null;
            }

            @Override
            protected Void doInBackground(Void... params) 
            {
                InputStream is;
                try {
                     is = (InputStream) new URL(url).getContent();
                     _infoImageDrawable = Drawable.createFromStream(is, "");
                } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
                return null;                
            }

            @Override
            protected void onPostExecute(Void result) 
            {
            super.onPostExecute(result);
            marker.showInfoWindow();
            }
        }.execute();
    }
}

希望这会有所帮助!!