Android:如何显示Google Places API中的照片

Gid*_*zer 2 android google-places

我有一个应用程序,它会从Google Places API返回一个地点列表.当用户单击地图上的标记(表示地点)时,会弹出一个对话框,其中包含有关该地点的详细信息.我还要显示该地方的照片(如果存在).

我从这里了解Place Photos API:https://developers.google.com/places/documentation/photos#place_photo_response

我可以从Place Details响应中检索photo_reference,我可以将它传递给Place Photos API请求,以及sensor,maxwidth/maxheight和我的API密钥.我遇到的问题是从响应中获取照片.如何将照片对象检索为位图,以便我可以在对话框中显示它.

Gid*_*zer 6

经过几个小时的挫折,我找到了解决方案.

通过调用getContentType(来自API的响应,我能够识别出响应只是给定类型的图像.在我的情况下,我发现它是"text/jpeg".您可以InputStream通过调用来检索内容getContent().

BitmapFactoryclass允许你解码InputStream成a Bitmap.

Bitmap photo = BitmapFactory.decodeStream(request.execute().getContent());
Run Code Online (Sandbox Code Playgroud)

为了显示这个图像,我首先需要设置一个自定义视图DialogBuilder.

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

接下来,我得到照片响应并将其添加到ImageView布局中(已在XML中定义):

LinearLayout ll = (LinearLayout) view.findViewById(R.id.dialogLayout);
ImageView imageView = (ImageView) ll.findViewById(R.id.placePhoto);
imageView.setImageBitmap(photo);
Run Code Online (Sandbox Code Playgroud)

请务必注意,"地方详细信息"响应最多可以发回10张照片.您无法保证将"最佳"照片发回.在我的情况下,我采取第一个.