【发布时间】:2017-05-02 09:08:21
【问题描述】:
我在使用 Google Places API 将 URL 中的图片加载到我的 Android 应用时遇到问题。
我得到了 API 工作的部分,如果我在浏览器中输入 URL,我可以看到图像。代码在以下行中失败: 位图 myBitmap = BitmapFactory.decodeStream(input);
奇怪的是,它只是直接跳转到catch子句中的return null,而不执行println命令。
我还应该解释一下,我使用了两个 URL,因为 google API 中的第一个 URL 是重定向页面。
我认为问题在于正在加载的最终页面实际上不是“真实”图像(我的意思是它看起来像 https://lh4.googleusercontent.com/XXX/s1600-w400/ 而不是类似的东西 http://something/image.jpg)
下面是我使用的整个方法:
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;
}
}
感谢您的帮助。
【问题讨论】:
-
用毕加索试试square.github.io/picasso很简单
-
问题已解决。这真的很有帮助,谢谢!
标签: android imageview google-places-api