【问题标题】:Retrieving Image from Google Places API in Android从 Android 中的 Google Places API 检索图像
【发布时间】:2018-07-21 05:02:08
【问题描述】:

在一个片段中,我实现了一个 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);
}

任何建议将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    您在 onCompleteListener 完成之前返回 bitmapArray.get(0);。您应该在片段顶部设置一个变量bitmapArray,并在照片完成侦听器完成后将位图添加到 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
    
                    }
                });
            }
        });
    }
    }
    

    【讨论】:

    • 谢谢!我实现了这个并调用了 getPhotos(id);位图 = bitmapArray.get(0);在 onPlaceSelected 但我仍然得到 0 位图数组的大小和位图的空指针异常。
    • 如果您正在更新 imageView 或将位图上传到服务器,您应该在 photoResponse.addOnCompleteListener 中调用该函数,以便在位图任务完成之前不会调用处理位图的函数。
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多