Android: How to display photos from Google Places API

家住魔仙堡 提交于 2019-12-06 15:54:56

I have found a solution after a couple of hours of frustration.

By calling getContentType() on the response back from the API, I was able to identify that the response is simply an image of a given type. In my case I found it to be "text/jpeg". You can retrieve the InputStream of the content by calling getContent().

BitmapFactory class allows you to decode an InputStream into a Bitmap.

Bitmap photo = BitmapFactory.decodeStream(request.execute().getContent());

In order to display this image, I first needed to set a custom view for the DialogBuilder.

LayoutInflater factory = LayoutInflater.from(mapView.getContext());
final View view = factory.inflate(R.layout.dialog_layout, null);
dialog.setView(view);

Next, I get the photo response and add it to the ImageView in the layout (which is already defined in the XML):

LinearLayout ll = (LinearLayout) view.findViewById(R.id.dialogLayout);
ImageView imageView = (ImageView) ll.findViewById(R.id.placePhoto);
imageView.setImageBitmap(photo);

It is important to note that the Place Details response can send back up to 10 photos. You can't guarantee that the "best" photo will be sent back. In my case, I take the first one.

The latest release of Google Play Services (7.8) includes the ability to get the photos associated with a Place - see here for the javadoc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!