我目前在Android应用中有一项功能,可让我发布评论并附上照片。评论将上传到Cloud Firestore,并通过其下载URL引用对Firebase存储的评论。
我遇到的问题是,在第一次调用该方法时,它似乎没有检索下载URL,但是当我进行第二次调用时,它成功检索到它。下面我已经包含了这个方法,只需在按钮点击监听器中调用即可。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_comments);
submitComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newComment();
}
});
private void newComment() {
Date date = new Date();
EventComment mcomment = new EventComment();
mcomment.setComment(commentText.getText().toString());
mcomment.setDate_created(date);
// mcomment.setUsername(firebaseAuth.getCurrentUser().getEmail());
String commentID = Long.toHexString(Double.doubleToLongBits(Math.random()));
StorageReference filePath = firebaseStorage.child("Photos").child(imageUri.getLastPathSegment());
filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
if(taskSnapshot.getDownloadUrl() != null){
imgDownloadUri = taskSnapshot.getDownloadUrl();
} else if(taskSnapshot.getMetadata().getDownloadUrl() != null) {
imgDownloadUri = taskSnapshot.getMetadata().getDownloadUrl();
}
Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
if (imgDownloadUri != null) {
mcomment.setImageUri(imgDownloadUri.toString());
}
firestore.collection("events").document(event_id).collection("comments").document(commentID)
.set(mcomment, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(EventCommentsActivity.this, "Comment posted successfully", Toast.LENGTH_SHORT).show();
}
});
}
firestore.collection("events").document(event_id).collection("comments").document(commentID)
.set(mcomment, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(EventCommentsActivity.this, "Comment posted successfully", Toast.LENGTH_SHORT).show();
}
});
我已经多次调试了这个,我不确定是什么导致了这个。或许它可以做回调的工作方式,并在一个方法中使用它可能会导致问题?
任何反馈都将不胜感激。
干杯
答案 0 :(得分:1)
我在代码中解释,看看代码内的注释,如果您有任何疑问,请询问!
private void newComment() {
StorageReference filePath = firebaseStorage.child("Photos").child(imageUri.getLastPathSegment());
//start progessbar here
filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
if(taskSnapshot.getDownloadUrl() != null){
imgDownloadUri = taskSnapshot.getDownloadUrl();
} else if(taskSnapshot.getMetadata().getDownloadUrl() != null) {
imgDownloadUri = taskSnapshot.getMetadata().getDownloadUrl();
}
if (imgDownloadUri != null) {
//close progessbar here
mcomment.setImageUri(imgDownloadUri.toString());
}
Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
firestore.collection("events").document(event_id).collection("comments").document(commentID)
.set(mcomment, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(EventCommentsActivity.this, "Comment posted successfully", Toast.LENGTH_SHORT).show();
}
});
}