如何将图像从url加载到InfoWindowAdapter android?图像未显示
我试图在“InfoWindowAdapter”中显示url中的图像,我有以下代码,但没有显示图像。请问怎么了如何将图像从url加载到InfoWindowAdapter android?图像未显示,android,google-maps,android-asynctask,imageview,infowindow,Android,Google Maps,Android Asynctask,Imageview,Infowindow,我试图在“InfoWindowAdapter”中显示url中的图像,我有以下代码,但没有显示图像。请问怎么了 public class ObjectInfoWindow implements GoogleMap.InfoWindowAdapter { private Activity activity; private HashMap<String, LostObject> markers; private Marker markerShowingInfoW
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;
}
首先,我没有看到任何存储URL路径/地址的变量。因此,程序无法获取图像的位置并将其显示在信息窗口中。有两种方法可以做到这一点:
- 一种是使用Universal Image Loader,这是一个可以嵌入到应用程序项目中的库。这与使用custominfoadapter在应用程序代码中所做的操作非常相似。有关代码的完整实现,请参阅此
- 另一种方法是使用单独的线程(异步任务是最好的选择)。这将在后台从URL加载图像,并以同步方式将其粘贴到custominfowindow中李>
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();
}
}
protected void HandleMarker单击(最终标记,最终字符串url){
新建异步任务()
{
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
_infoImageDrawable=null;
}
@凌驾
受保护的Void doInBackground(Void…参数)
{
输入流为;
试一试{
is=(InputStream)新URL(URL).getContent();
_infoImageDrawable=Drawable.createFromStream(为“”);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
marker.showInfoWindow();
}
}.execute();
}
}
希望这会有帮助 你解决问题了吗?是的,我解决问题了。你是如何解决问题的???
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();
}
}