如何在 infoWindow 中显示来自 URL 的动态 ImageView

How to display dynamic ImageView from URL in infoWindow

我正在尝试下载图像并将其显示为 ImageView 在我的 infoWindow 中,使用 InfoWindowsAdapter ,我把所有getInfoContents() 方法中的必要代码,但我无法获取图像,实际上我可以获取但仍然无法显示。 正如我在文档中阅读的那样,我不得不回忆起设置下载图像的处理方式,我必须使用 showInfoWindow() 方法,因为这是唯一的方法。我试图在 return 之前的 getInfoContents() 中使用它,但是它阻止了我的应用程序,需要帮助!

这是我的 getInfoContents() :

  public View getInfoContents(Marker marker) {
  // set the view
  View v = (View) getLayoutInflater().inflate(R.layout.info_window,null);

  //assign values to the view field

   ImageView imagePlace =(ImageView) v.findViewById(R.id.imageview1);
   TextView tvLocality = (TextView) v.findViewById(R.id.tv_place);

  //setting values with the parameter


  Picasso.with(getApplicationContext()).load(url_image_place).into(imagePlace);
  tvLocality.setText(marker.getTitle());

 //marker.showInfoWindow();
 return v;


}

这是我的 downloadIcon() :

 private Bitmap downloadIcon(String iconURL) {
    Bitmap bmImg = null;
    try {
        URL url = new URL(iconURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bmImg = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        e.printStackTrace();
        bmImg = null;
    }

    return bmImg;
} 

编辑: 我用picasso下载我的图片,但是图片只有在第二次点击后才会显示,任何使用相同图片的标记的信息窗口都是第一次获取图片,这意味着必须先下载图片!!我编辑了我的 getInfoContents() 并且我不再使用 downloadIcon() 了!

使用Picasso,这非常容易。简单地写:

String url = ...;//Your url
ImageView imageView; //Your ImageView
Picasso.with(context).load(url).into(imageView);

或者,如果你想自己做,你可以实现一个 AsyncTask:

定义:

private class ImageDownloader extends AsyncTask {

        @Override
        protected Bitmap doInBackground(String... param) {
            return downloadIcon(param[0]);
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            //Handle your result, i.e. your bitmap 
        }
}

执行:

new ImageDownloader().execute(iconURL);

您不应该使用主 UI 线程下载图像,因此您需要另一个线程来执行此操作,例如:

public void setImage(final ImageView img, final String iconURL)
{

  Thread thread = new Thread(new Runnable() {

     @Override
     public void run()
     {
        Bitmap bmImg;               

        try {

              URL url = new URL(iconURL);
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setDoInput(true);
              conn.connect();
              InputStream is = conn.getInputStream();
              bmImg = BitmapFactory.decodeStream(is); 

        } 
        catch (IOException e) {
             e.printStackTrace();
             bmImg = null;
        }


        //after downloading, then talk to main ui
        runOnUiThread(new Runnable()
        {                      
              @Override
              public void run() 
              {
                   img.setImageBitmap(bmImg);

              } 
        }


     }}).start();

 }

希望对您有所帮助!