[英]Retrieving Image from Google Places API in Android
在一个片段中,我实现了一个Google PlaceAutoCompleteFragment。 选择一个地点后,我想将该地点的图像上传到Parse服务器,因此我一直在遵循本指南 。
在OnCreateView()中,我使用初始化了GeoDataClient mGeoDataClient
mGeoDataClient = Places.getGeoDataClient(getActivity());
我将正确的placeId传递给以下方法,调试器说photoMetadataResponse是zzu @ 6303 (我认为还可以吗?)。 似乎从未执行过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);
}
任何意见,将不胜感激!
您正在返回bitmapArray.get(0);
在onCompleteListener完成之前。 您应该在片段的顶部设置一个变量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.