gpt4 book ai didi

java - .setValue 和 .addValueListener 错误

转载 作者:行者123 更新时间:2023-12-02 10:53:08 25 4
gpt4 key购买 nike

关于如何将图像保存在 Firebase 存储中,我遇到一些编码错误。并出现 2 个错误...有人可以帮助我...我的 logcat 如下...

1) 错误:找不到符号方法 setValue(String)

2) 错误:找不到符号方法addValueEventListener()

storageImage.child("Tuition Image").setValue(downloadUrl)
.addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task)
{
if (task.isSuccessful())
{
Intent ViewIntent = new Intent(AddAdsActivity.this, AddAdsActivity.class);
ViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(ViewIntent);
finish();

Toast.makeText(AddAdsActivity.this, "Tuition image has been successfully stored in Firebase Database", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}

else
{
String message = task.getException().getMessage();
Toast.makeText(AddAdsActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});

我的第二个错误...

storageImage.child(currentUserID).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
image = dataSnapshot.child("Tuition Image").getValue().toString();
Picasso.get().load(image).placeholder(R.drawable.camera).into(TuitionImage);
}
}

@Override
public void onCancelled(DatabaseError databaseError)
{

}
});

最佳答案

解决方案:

首次上传图片:

private void uploadImageAndGetURL(String ImageId) {

final StorageReference filePath = storageImage.child(ImageId + ".jpg");

UploadTask uploadTask = filePath.putFile(imageUri);

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();
}
// Continue with the task to get the download URL
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {

if (task.isSuccessful()) {

downloadURL = task.getResult().toString();

storeDataToFirebase();

} else {

Toast.makeText(AddAdsActivity.this, "There has bean a problem in the database.", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();

}
}
});

}

然后上传数据:

private void storeDataToFirebase() {

String id = databaseAdvertisement.push().getKey();
Advertisement advertisement = new Advertisement(id, downloadURL, tuitioname, providername, providergender, tuitionaddress, tuitionyear, tuitioncontactnumber, tuitionemail, tuitionsubject, tuitionprice, tuitionclasssize);

databaseAdvertisement.child(id).setValue(advertisement)
.addOnSuccessListener(new OnSuccessListener<Void>()
{
@Override
public void onSuccess(Void aVoid)
{
Toast.makeText(AddAdsActivity.this, "Advertisement added successfully!", Toast.LENGTH_SHORT).show();

Intent ViewAdsIntent = new Intent(AddAdsActivity.this, ViewAdsActivity.class);
ViewAdsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(ViewAdsIntent);
finish();

loadingBar.dismiss();
}
})
.addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Toast.makeText(AddAdsActivity.this, "Failed to save the advertisement data. Please try again.", Toast.LENGTH_LONG).show();
loadingBar.dismiss();
}
});

}

就是这样。

关于java - .setValue 和 .addValueListener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52008604/

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