我正在制作一个健身Android应用程序,我正在使用Firebase存储存储用户的进度图像。还可以使用Firebase存储中找到的图像的URL来引用Firebase数据库。
我正在尝试检索图片并在GridLayout中显示它们,但我们正在接受“绑定到类型错误”
我使用recyclerView显示图像。 我也在使用Picasso来加载图像。
照片活动
public class PhotosActivity extends BaseActivity {
@BindView(R.id.activity_photos_fab)
FloatingActionButton newPhoto;
private StorageReference mStorage;
private ProgressDialog mProgressDialog;
RecyclerView recyclerView;
FirebaseRecyclerAdapter adapter;
private static final int GALLERY_INTENT = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
ButterKnife.bind(this);
mStorage = FirebaseStorage.getInstance().getReference();
mProgressDialog = new ProgressDialog(this);
recyclerView = (RecyclerView) findViewById(R.id.activity_photos_recyclerView);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
mProgressDialog.setTitle("Uploading..");
mProgressDialog.show();
// Get the URI of the photo
Uri uri = data.getData();
StorageReference filepath = mStorage.child("Photos").child(userEmail).child(uri.getLastPathSegment());
Firebase photosReference = new Firebase(Utils.FIRE_BASE_PHOTOS_REFERENCE + userEmail);
// Add the photo to the database
// if successful then show a toast to say a photo has been added
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "You added a photo", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
});
String photoURL = filepath.getDownloadUrl().toString();
bus.post(new PhotoService.AddPhotoRequest(photoURL, userEmail));
}
}
@Override
protected void onResume() {
super.onResume();
Firebase photosReference = new Firebase(Utils.FIRE_BASE_PHOTOS_REFERENCE + userEmail);
Query sortQuery = photosReference.orderByKey();
adapter = new FirebaseRecyclerAdapter<Photo, PhotoViewHolder>(Photo.class,
R.layout.list_individual_photo,
PhotoViewHolder.class,
sortQuery) {
@Override
protected void populateViewHolder(PhotoViewHolder photoViewHolder, Photo photo, int i) {
photoViewHolder.populate(PhotosActivity.this, photo);
}
};
GridLayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 4);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
@Override
protected void onPause() {
super.onPause();
adapter.cleanup();
}
@OnClick(R.id.activity_photos_fab)
public void setNewPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_INTENT);
}
}
PhotoViewHolder
public class PhotoViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.list_individual_photo_imageView)
ImageView photoImage;
@BindView(R.id.list_individual_photo_progressBar)
ProgressBar photoProgressBar;
public PhotoViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void populate (Context context, Photo photo) {
itemView.setTag(photo);
Picasso.with(context).load(photo.getPhotoURl())
.fit()
.centerCrop()
.into(photoImage, new Callback() {
@Override
public void onSuccess() {
photoProgressBar.setVisibility(View.GONE);
}
@Override
public void onError() {
}
});
}
}
LivePhotoService
public class LivePhotoService extends BaseLiveService {
@Subscribe
public void getPhotos(final PhotoService.GetPhotoRequest request) {
final PhotoService.GetPhotoResponse response = new PhotoService.GetPhotoResponse();
response.valueEventListener = request.firebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
response.photo = dataSnapshot.getValue(Photo.class);
if(response.photo != null) {
bus.post(response);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(application.getApplicationContext(), firebaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
照片对象
public class Photo {
private String photoID;
private String photoURl;
private HashMap<String, Object> photoDate;
private String ownerEmail;
public Photo(String photoID, String photoURl, HashMap photoDate, String ownerEmail) {
this.photoID = photoID;
this.photoURl = photoURl;
this.photoDate = photoDate;
this.ownerEmail = ownerEmail;
}
public String getPhotoID() {
return photoID;
}
public String getPhotoURl() {
return photoURl;
}
public HashMap getPhotoDate() {
return photoDate;
}
public String getOwnerEmail() {
return ownerEmail;
}
}
答案 0 :(得分:0)
考虑使用Firebase-UI Android库,它可以直接从存储引用加载图像。在你的情况下,它看起来像这样
我不确定Picasso是否受支持,但你可以使用Glide 例如:
mStorageRef = FirebaseStorage.getInstance().getReference();
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(mStorageRef + "/Photos/" + userId)
.error(R.drawable.default)
.into(imageView);