구글에서 사진을 받아와서 recyclerview로 넘겨주는 작업을 하고싶습니다.
public void sendPlacedata(){
myDataset.clear();
for(int i =0; i < IDarrayList.size(); i++) {
final String PID = IDarrayList.get(i);
Places.GeoDataApi.getPlaceById(mGoogleApiClient, PID)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer place) {
if (place.getStatus().isSuccess() && place.getCount() > 0) {
final com.google.android.gms.location.places.Place myPlace = place.get(0);
new PhotoTask(mRecyclerView.getWidth(), mRecyclerView.getHeight()).execute(PID);
myDataset.add(new mainitem(GoogleImage));
for문으로 PlaceId 별로 이미지를 받아와서 넘겨주는 방식인데요
private class PhotoTask extends AsyncTask<String, Void, PhotoTask.AttributedPhoto> {
private int mHeight;
private int mWidth;
public PhotoTask(int width, int height) {
mHeight = height;
mWidth = width;
}
@Override
protected AttributedPhoto doInBackground(String... params) {
if (params.length != 1) {
return null;
}
final String placeId = params[0];
AttributedPhoto attributedPhoto = null;
PlacePhotoMetadataResult result = Places.GeoDataApi
.getPlacePhotos(mGoogleApiClient, placeId).await();
if (result.getStatus().isSuccess()) {
PlacePhotoMetadataBuffer photoMetadataBuffer = result.getPhotoMetadata();
if (photoMetadataBuffer.getCount() > 0 && !isCancelled()) {
// Get the first bitmap and its attributions.
PlacePhotoMetadata photo = photoMetadataBuffer.get(0);
CharSequence attribution = photo.getAttributions();
// Load a scaled bitmap for this photo.
Bitmap image = photo.getScaledPhoto(mGoogleApiClient, mWidth, mHeight).await()
.getBitmap();
attributedPhoto = new AttributedPhoto(attribution, image);
}
// Release the PlacePhotoMetadataBuffer.
photoMetadataBuffer.release();
}
return attributedPhoto;
}
@Override
protected void onPostExecute(AttributedPhoto attributedPhoto) {
if (attributedPhoto != null) {
// Photo has been loaded, display it.
GoogleImage = attributedPhoto.bitmap;
}
}
class AttributedPhoto {
public final CharSequence attribution;
public final Bitmap bitmap;
public AttributedPhoto(CharSequence attribution, Bitmap bitmap) {
this.attribution = attribution;
this.bitmap = bitmap;
}
}
}
이건 구글에 나와있는 소스를 조금 바꿔서 적용한 것입니다.
처음에는 ArrayList에 다 담아서 차례차례 뿌려주려고 했는데 bitmap 용량이 생각보다 커서 메모리에 문제가
발생하더라구요 그래서 AsyncTask로 받을 때마다 넘겨주려고 하는데 이미지가 하나로만 recyclerview가 채워집니다.
로그로 확인해보니 마지막으로 전달된 ID의 이미지도 아니라서 무엇이 문제인지 모르겠네요
아시는 분이 계시면 꼭 좀 알려주시면 감사하겠습니다.