我正尝试从Firebase数据库中检索所有用户帖子,并希望借助Recycler View和Firebase Recycler Adapter在Main Activity上显示它。 但是在成功将图像上传到Firebase数据库之后,我可以在firebase控制台的存储部分看到上传的图像,但是在数据库部分显示的是-
com.google.android.gms.tasks.zzu@3c50f45
所以我认为问题出在这行代码:
downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();
代码:
private Uri ImageUri;
private StorageReference PostsImagesRefrence;
private DatabaseReference UsersRef, PostsRef;
PostsImagesRefrence = FirebaseStorage.getInstance().getReference();
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
StorageReference filePath = PostsImagesRefrence.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg");
filePath.putFile(ImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>()
{
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
{
if(task.isSuccessful())
{
downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();
Toast.makeText(PostActivity.this, "image uploaded successfully to Storage...", Toast.LENGTH_SHORT).show();
SavingPostInformationToDatabase();
}
else
{
String message = task.getException().getMessage();
Toast.makeText(PostActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
}
}
});
设计视图和实际输出图像:
请尝试为我提供正确的代码,作为我的新手并且不了解Android Studio并希望完成此College Project,请为我提供帮助。
答案 0 :(得分:0)
尝试更改
String userFullName = dataSnapshot.child("fullname").getValue().toString();
String userProfileImage = dataSnapshot.child("profileimage").getValue().toString();
收件人
String userFullName = dataSnapshot.child("fullname").getValue(String.class);
String userProfileImage = dataSnapshot.child("profileimage").getValue(String.class);
编辑:尝试
dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri downloadUrl)
{
downloadUrlString = downloadUrl.toString();
}
});
答案 1 :(得分:0)
解决了这个问题,我正在使用旧的方法将图像存储到Firebase数据库中。
已进行以下更改以解决此问题:
final StorageReference filePath = PostsImagesRefrence.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg");
final UploadTask uploadTask =filePath.putFile(ImageUri);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
String message =e.toString();
Toast.makeText(PostActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
{
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
Toast.makeText(PostActivity.this, "Image uploaded successfully to Storage...", Toast.LENGTH_SHORT).show();
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>()
{
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
{
if (!task.isSuccessful())
{
throw task.getException();
}
downloadUrl= filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>()
{
@Override
public void onComplete(@NonNull Task<Uri> task)
{
if (task.isSuccessful())
{
downloadUrl = task.getResult().toString();
Toast.makeText(PostActivity.this, "Stored Image Successfully to Database", Toast.LENGTH_SHORT).show();
SavingPostInformationToDatabase();
}
}
});
}