从GeoDataClient.getPlacePhotos()获取照片时的illegalStateException

时间:2017-09-14 07:21:56

标签: android google-places-api illegalstateexception

我指的是Google Places photo android api。 我正在使用RecyclerView Adapter的onBindViewHolder下面的代码。 它抛出非法国家例外的一半时间。请帮忙。

 final Task<PlacePhotoMetadataResponse> photoMetadataResponse = mGeoDataClient.getPlacePhotos(placeId);
        photoMetadataResponse.addOnCompleteListener(new OnCompleteListener<PlacePhotoMetadataResponse>() {
            @Override
            public void onComplete(@NonNull Task<PlacePhotoMetadataResponse> task) {
                // Get the list of photos.
                PlacePhotoMetadataResponse photos = task.getResult();
                // Get the PlacePhotoMetadataBuffer (metadata for all of the photos).
                PlacePhotoMetadataBuffer photoMetadataBuffer = photos.getPhotoMetadata();
                // Get the first photo in the list.
                PlacePhotoMetadata photoMetadata = photoMetadataBuffer.get(0);
                // Get the attribution text.
                CharSequence attribution = photoMetadata.getAttributions();
                // Get a full-size bitmap for the photo.
                Task<PlacePhotoResponse> photoResponse = mGeoDataClient.getPhoto(photoMetadata);
                photoResponse.addOnCompleteListener(new OnCompleteListener<PlacePhotoResponse>() {
                    @Override
                    public void onComplete(@NonNull Task<PlacePhotoResponse> task) {
                        PlacePhotoResponse photo = task.getResult();
                        Bitmap bitmap = photo.getBitmap();
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        Glide.with(mContext)
                                .load(stream.toByteArray())
                                .asBitmap()
                                .error(R.drawable.cast_album_art_placeholder)
                                .centerCrop()
                                .thumbnail(.2f)
                                .into(holder.placeImage);

                    }
                });
            }
        });

StackTrace:

E/UncaughtException: java.lang.IllegalStateException
                                                                at com.google.android.gms.common.internal.zzbp.zzbg(Unknown Source)
                                                                at com.google.android.gms.common.data.zzc.zzbu(Unknown Source)
                                                                at com.google.android.gms.common.data.zzc.<init>(Unknown Source)
                                                                at com.google.android.gms.location.places.internal.zzav.<init>(Unknown Source)
                                                                at com.google.android.gms.location.places.internal.zzar.<init>(Unknown Source)
                                                                at com.google.android.gms.location.places.PlacePhotoMetadataBuffer.get(Unknown Source)

2 个答案:

答案 0 :(得分:4)

即使这个问题已经存在了将近一年,并且由于给出的答案不能解决我的错误,我还是会分享我的解决方案:

// Get the first photo in the list.
if (photoMetadataBuffer.getCount() > 0) {
    PlacePhotoMetadata photoMetadata = photoMetadataBuffer.get(0);
// continue with your code
}

这是因为photoMetadataBuffer不为空。 但是是的,该错误仅发生在没有图片的地方。

答案 1 :(得分:3)

我很确定问题是您的应用程序崩溃了,因为您试图从没有要显示的照片的位置检索照片。在尝试检索photoMetadataBuffer.get(0)中的第一张照片之前,您必须进行空检查。这是一个很好的例子,说明Google的文档在提供的示例代码中有些不完整。您应该具有以下内容:

// Get the first photo in the list.
if (photoMetadataBuffer != null) {
    PlacePhotoMetadata photoMetadata = photoMetadataBuffer.get(0);
// continue with your code
}

如果photoMetadataBuffer为null,则无法显示照片,您可以正确处理应用程序逻辑,例如加载默认图像,向用户提供反馈或不显示ImageView。