在Android中从Google Places API检索图像

时间:2018-07-21 05:02:09

标签: java android android-fragments google-places-api

在一个片段中,我实现了一个Google PlaceAutoCompleteFragment。选择地点后,我想将该地点的图片上传到Parse服务器,因此我一直关注this guide

在OnCreateView()中,我已经使用

初始化了GeoDataClient mGeoDataClient。
mGeoDataClient = Places.getGeoDataClient(getActivity());

我将正确的placeId传递给以下方法,调试器说photoMetadataResponse是 zzu @ 6303 (我认为可以吗?)。似乎从未执行过onComplete,并且bitmapArray的末尾大小为0,因此 bitmap 返回为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);
}

任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

您将在onCompleteListener完成之前返回bitmapArray.get(0);。您应该在片段的顶部设置一个变量bitmapArray,并在photo completeListener完成后将位图添加到ArrayList中。

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

                }
            });
        }
    });
}
}