In one fragment, I have implemented a Google PlaceAutoCompleteFragment. When a place is selected, I'd like to upload an image of that place to a Parse server, so I've been following this guide .
In OnCreateView(), I've initialized the GeoDataClient mGeoDataClient using
mGeoDataClient = Places.getGeoDataClient(getActivity());
I'm passing the correct placeId into the following method, and the debugger says that photoMetadataResponse is zzu@6303 (which I assume is okay?). It seems like onComplete is never being executed, and bitmapArray has size 0 at the end so bitmap is returned as null.
// Request photos and metadata for the specified place.
private Bitmap getPhotos(String placeId) {
final ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
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();
bitmapArray.add(bitmap); // Add a bitmap
}
});
}
});
return bitmapArray.get(0);
}
Any advice would be appreciated!
You are returning bitmapArray.get(0);
before the onCompleteListener is complete. You should set a variable bitmapArray
at the top of your fragment and add bitmap to the ArrayList once the photo completeListener is complete.
public class ExampleFragment extends Fragment {
//Google places
protected GeoDataClient mGeoDataClient;
//vars
prvivate ArrayList<Bitmap> bitmapArray = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.example_fragment, container, false);
mGeoDataClient = Places.getGeoDataClient(ExampleFragment.this, null);
return view;
}
// Request photos and metadata for the specified place.
private void getPhotos(String placeId) {
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();
bitmapArray.add(bitmap); // Add a bitmap to array
//handle the new bitmap here
}
});
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.