Search code examples
androidgoogle-places-apiillegalstateexception

illegalStateException while getting photo from GeoDataClient.getPlacePhotos()


I am referring Google Places photo android api. I am using below code in onBindViewHolder of RecyclerView Adapter. Half the time it throws Illegal State exception. Please help.

 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)

Solution

  • I am fairly certain that the issue is that your application is crashing due to the fact that you are attempting to retrieve a photo from a location that does not have a photo to display. You must do a null check before attempting to retrieve the first photo in your photoMetadataBuffer.get(0). This is a good example of how Google's documentation is somewhat incomplete from the example code that is provided. You should have something like the following:

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

    If the photoMetadataBuffer is null, then there isn't a photo to be displayed and you can handle your application logic appropriately, such as loading a default image, giving feedback to the user, or not displaying the ImageView.