[英]Retrieving Image from Google Places API in Android
In one fragment, I have implemented a Google PlaceAutoCompleteFragment. 在一个片段中,我实现了一个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 .
选择一个地点后,我想将该地点的图像上传到Parse服务器,因此我一直在遵循本指南 。
In OnCreateView(), I've initialized the GeoDataClient mGeoDataClient using 在OnCreateView()中,我使用初始化了GeoDataClient mGeoDataClient
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?). 我将正确的placeId传递给以下方法,调试器说photoMetadataResponse是zzu @ 6303 (我认为还可以吗?)。 It seems like onComplete is never being executed, and bitmapArray has size 0 at the end so bitmap is returned as null.
似乎从未执行过onComplete,并且bitmapArray的末尾大小为0,因此位图返回为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);
您正在返回
bitmapArray.get(0);
before the onCompleteListener is complete. 在onCompleteListener完成之前。 You should set a variable
bitmapArray
at the top of your fragment and add bitmap to the ArrayList once the photo completeListener is complete. 您应该在片段的顶部设置一个变量
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
}
});
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.