gpt4 book ai didi

android - 在 RecyclerView 中显示 firebase 存储图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:44 25 4
gpt4 key购买 nike

您好,我之前问过一个关于使用 Firebase 数据库中的 Base64 显示图像的问题。建议使用 firebase 存储。我重新编写了我的代码,所有内容都按原样加载到 firebase 存储和数据库中。我遇到的问题是,当我保存数据时,没有任何内容填充到我的 recyclerview 中。一切都是空白。

如果您能为我提供任何帮助以使其正常工作,那就太好了。谢谢。

编辑:在我的 Activity 课上,我调用了一个按钮点击监听器来激活相机。拍照后,它会保存到 Firebase 存储中。然后我从 Storage 下载图像并将其显示在 recyclerview 中。我理解上传部分,但我很难理解下载和显示部分。谢谢。

viewholder:绑定(bind)方法

public void bind (ImageProgress progress){

Glide.with(activity).load("").into(image);
}
}

适配器

  @Override
public ProgressViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
return new ProgressViewHolder(activity, activity.getLayoutInflater().inflate(R.layout.weight_progress_list, parent, false));

主要 Activity 类

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weight_progress);

saveButton = (Button) findViewById(R.id.saveButton);
progressStatusEditText = (EditText) findViewById(R.id.progressStatusEditText);
progressList = (RecyclerView) findViewById(R.id.progressList);
mImageButton = (ImageButton) findViewById(R.id.takePictureButton);
capturedImage=(ImageView)findViewById(R.id.capturedImageView);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
progressList.setHasFixedSize(false);
progressList.setLayoutManager(layoutManager);

//take picture button
mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openCamera();
}
});


mDatabaseReference = database.getReference("Progress");

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//get current user
FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
ImageProgress progress = new ImageProgress(uid, progressStatusEditText.getText().toString());
mDatabaseReference.push().setValue(progress);
progressStatusEditText.setText("");
}
});
}

private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

Uri cameraImageURI = data.getData();
//reference where images will be stored
mStorageReference = storage.getReference("Progress Images");
//reference to store file
final StorageReference cameraImageRef = mStorageReference.child(cameraImageURI.getLastPathSegment());
//upload to firebase storage
cameraImageRef.putFile(cameraImageURI)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
progressStatusEditText.setText(downloadUrl.toString());
}
});
}

最佳答案

我建议您在 RecyclerView.ViewHolder 上添加 setImage 方法,并将图像的 url 存储在您在 FirebaseRecyclerAdapter 中检索的对象中。

就我而言,我使用的是 Glide管理我的应用程序的图像:

我的 ViewHolder 示例:

public class UserListViewHolder extends RecyclerView.ViewHolder {
private final View mView;
private static final int POST_TEXT_MAX_LINES = 6;
private ImageView mIconView;
private TextView mAuthorView;
private UserClickListener mListener;

public UserListViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIconView = (ImageView) mView.findViewById(R.id.user_image_profile);
mAuthorView = (TextView) mView.findViewById(R.id.user_text_profile);
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onUserClick();
}
});
}

public void setIcon(String url) {
GlideUtil.loadProfileIcon(url, mIconView);//
}

public void setPostClickListener(UserClickListener listener) {
mListener = listener;
}


public void setText(final String text) {
if (text == null || text.isEmpty()) {
mAuthorView.setVisibility(View.GONE);
return;
} else {
mAuthorView.setVisibility(View.VISIBLE);
mAuthorView.setText(text);
mAuthorView.setMaxLines(POST_TEXT_MAX_LINES);
}
}

public interface UserClickListener {
void onUserClick();
}
}

这是我的数据库调用:

 private FirebaseRecyclerAdapter<Person, UserListViewHolder> getFirebaseRecyclerAdapter(Query query) {
return new FirebaseRecyclerAdapter<Person, UserListViewHolder>(
Person.class, R.layout.user_row_item, UserListViewHolder.class, query) {

@Override
protected void populateViewHolder(UserListViewHolder viewHolder, Person model, int position) {
setupUser(viewHolder, model, position, null);
}

};
}

private void setupUser(final UserListViewHolder UserViewHolder, final Person user, final int position, final String inPostKey) {
final String postKey;
if (mAdapter instanceof FirebaseRecyclerAdapter) {
postKey = ((FirebaseRecyclerAdapter) mAdapter).getRef(position).getKey();
} else {
postKey = inPostKey;
}
FirebaseUtil.getUsersRef().child(postKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
UserViewHolder.setPostClickListener(new UserListViewHolder.UserClickListener() {
@Override
public void onUserClick() {
mGroup.addUserToGroup(dataSnapshot.getKey());
}
});
UserViewHolder.setIcon(dataSnapshot.child("photoUrl").getValue(String.class));
UserViewHolder.setText(dataSnapshot.child("displayName").getValue(String.class));

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

您只需按照从 Firebase 检索数据时设置其他值的方式设置图像,但在将图像上传到云端后使用您已经为该图像生成的新 URL。

也许你可以找到另一种方法来设置 url,但我建议你将它保留为自定义对象中的另一个参数,如果你想多次检索该数据即使你没有上传图像也是动态的.

关于android - 在 RecyclerView 中显示 firebase 存储图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39499659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com