您好我之前问过一个关于使用Firebase数据库中的Base64显示图像的问题。建议使用firebase存储。我重新编写了代码,所有内容都加载到firebase存储和数据库中。我遇到的问题是当我保存数据时,没有任何内容填充到我的recyclerview中。一切都是空白的。
你可以提供给我的任何帮助,这将是很好的。感谢。
编辑:在我的活动课程中,我调用了一个按钮clicklistener来激活相机。拍摄照片后,它将保存到Firebase存储。然后我从存储中下载图像并将其显示在recyclerview中。我理解上传部分,但我很难理解下载和显示部分。感谢。
viewholder:绑定方法
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));
主要活动类
@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());
}
});
}
答案 0 :(得分:4)
我建议您在RecyclerView.ViewHolder
上添加setImage方法,并将图片的网址存储在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检索数据时设置其他值的方式相同,但在将图片上传到云端后使用已生成的newURL。
也许您可以找到另一种方法来设置网址,但我建议您将其保留为自定义对象中的其他参数,如果您想要多次检索该数据,即使您没有上传图片,也要保持动态
答案 1 :(得分:1)
为此目的已经有正式实施。
https://github.com/firebase/quickstart-android/tree/master/storage
基本思路是将图像上传到firebase存储,并将图像URL存储到数据库,然后通过此链接下载。
在MyDownloadService.java类中使用了mStorageRef.child(downloadPath).getStream方法,但我用getBytes方法实现了不处理输入流的方法。因为可以轻松地用Glide加载字节。任务完成后,它会发送一个包含字节的广播意图。您可以在任何地方收听广播接收器,以便在下载时加载图像。
public class DownloadService extends Service{
private StorageReference mStorageRef;
@Override
public void onCreate() {
super.onCreate();
// Initialize Storage
mStorageRef = FirebaseStorage.getInstance().getReference();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ACTION_DOWNLOAD.equals(intent.getAction())) {
mStorage.child(downloadPath).getBytes(10*1024*1024).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Log.d(TAG, "download:SUCCESS " + bytes.length);
// Send success broadcast with number of bytes downloaded
Intent broadcast = new Intent(ACTION_COMPLETED);
broadcast.putExtra(EXTRA_DOWNLOAD_BYTES, bytes);
broadcast.putExtra(EXTRA_DOWNLOAD_INDEX, uid);
LocalBroadcastManager.getInstance(getApplicationContext())
.sendBroadcast(broadcast);
// Mark task completed
taskCompleted();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.w(TAG, "download:FAILURE", exception);
// Send failure broadcast
Intent broadcast = new Intent(ACTION_ERROR);
broadcast.putExtra(EXTRA_DOWNLOAD_PATH, downloadPath);
LocalBroadcastManager.getInstance(getApplicationContext())
.sendBroadcast(broadcast);
// Mark task completed
taskCompleted();
}
});
}
}