[英]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.