从Android中的Google Places加载图像
我在使用GooglePlacesAPI将URL中的图像加载到Android应用程序时遇到问题 我得到了API工作的部分,如果我在浏览器中键入URL,我可以看到图像。代码在以下行中失败: 位图myBitmap=BitmapFactory.decodeStreaminput 奇怪的是,它只是跳转到catch子句中的returnnull,而不执行println命令 我还应该解释一下,我使用了两个URL,因为GoogleAPI中的第一个URL是重定向页面 我认为问题在于加载的最后一个页面实际上不是一个真实的图像,我的意思是它看起来像 而不是像 下面是我使用的整个方法:从Android中的Google Places加载图像,android,imageview,google-places-api,Android,Imageview,Google Places Api,我在使用GooglePlacesAPI将URL中的图像加载到Android应用程序时遇到问题 我得到了API工作的部分,如果我在浏览器中键入URL,我可以看到图像。代码在以下行中失败: 位图myBitmap=BitmapFactory.decodeStreaminput 奇怪的是,它只是跳转到catch子句中的returnnull,而不执行println命令 我还应该解释一下,我使用了两个URL,因为GoogleAPI中的第一个URL是重定向页面 我认为问题在于加载的最后一个页面实际上不是一个真
public Bitmap getImageData(Place p) {
try {
String src= "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference="+
p.getPhoto_reference()+
"&key="+
this.context.getResources().getString(places_api);
Log.d("srcsrc", src);
URL url = new URL(src);
HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
HttpsURLConnection connection = (HttpsURLConnection) secondURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Bitmap exception" + e);
return null;
}
}
感谢您的帮助。像这样使用毕加索:
Picasso.with(context).load(src).into(imageView );
它将执行以下操作:
在适配器中处理ImageView回收和下载取消。
使用最少内存的复杂图像转换。
自动内存和磁盘缓存。尝试使用毕加索,它非常简单。那真的很有帮助,谢谢你!