2017-09-14 6 views
1

Je fais référence Google Places photo android api. J'utilise le code ci-dessous dans onBindViewHolder de l'adaptateur RecyclerView. La moitié du temps, il déclenche une exception d'état illégal. S'il vous plaît aider.illegalStateException lors de l'obtention de la photo de GeoDataClient.getPlacePhotos()

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) 

Répondre

1

Je suis assez certain que la question est que votre application se bloque en raison du fait que vous tentez de récupérer une photo à partir d'un endroit qui ne dispose pas d'une photo afficher. Vous devez faire une vérification null avant d'essayer de récupérer la première photo dans votre photoMetadataBuffer.get(0). Ceci est un bon exemple de la manière dont la documentation de Google est quelque peu incomplète à partir de l'exemple de code fourni. Vous devriez avoir quelque chose comme ce qui suit:

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

Si le photoMetadataBuffer est nul, alors il n'y a pas une photo à afficher et vous pouvez gérer votre logique d'application appropriée, comme le chargement d'une image par défaut, donner des commentaires à l'utilisateur, ou ne pas afficher le ImageView.