在将图像上传到Firebase存储后,我需要获取图像URL。之后,图片网址将与其他用户数据一起添加到我的Firebase数据库中。但是,该链接始终被视为abc,我想知道为什么它不会被
覆盖link = task.getResult().getStorage().getDownloadUrl().toString();
在uploadImage()
方法中。有人能帮我吗?
预先感谢:)
public class A extends AppCompatActivity {
private Button btnChoose, btnUpload;
private ImageView imageView;
private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;
private DatabaseReference mDatabase;
private String username;
private FirebaseStorage storage;
private StorageReference storageReference;
private String role = "therapist";
private FirebaseAuth mAuth;
private String email, psd, link = "abc";
private UploadTask upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("name");
email = extras.getString("email");
psd = extras.getString("psd");
}
btnChoose = (Button) findViewById(R.id.choosedevice);
btnUpload = (Button) findViewById(R.id.nexttothome);
imageView = (ImageView) findViewById(R.id.viewImage);
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
Signup();
}
});
compname = findViewById(R.id.compnameregister);
adr1 = findViewById(R.id.adr1register);
adr2 = findViewById(R.id.adr2register);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
mDatabase = FirebaseDatabase.getInstance().getReference();
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
private void uploadImage() {
if (filePath != null) {
final StorageReference ref = storageReference.child("images/" + UUID.randomUUID().toString());
ref.putFile(filePath).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo2.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
link = task.getResult().getStorage().getDownloadUrl().toString();
}
}
});
}
}
private void Signup() {
mAuth.createUserWithEmailAndPassword(email, psd).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
//check if successful
if (task.isSuccessful()) {
onAuthSuccess(task.getResult().getUser());
} else {
Toast.makeText(A.this, "Couldn't register, try again", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void onAuthSuccess(FirebaseUser user) {
String compname1 = compname.getText().toString().trim();
String adr11 = adr1.getText().toString().trim();
String adr21 = adr2.getText().toString().trim();
// Write new user
sendToDatabase(user.getUid(), username, role, link, compname1, adr11, adr21);
}
private void sendToDatabase(String userId, String username, String role, String link, String compname1, String adr11, String adr21) {
User thera = new User(username, role, link);
Company comp = new Company(compname1, adr11, adr21);
mDatabase.child("user").child(userId).setValue(thera);
mDatabase.child("company").child(userId).setValue(comp);
}
}
答案 0 :(得分:0)
您的 Signup()方法似乎在异步任务在uploadImage()中运行之前被触发。尝试将Signup()放入uploadImage()的 onComplete()中。
就像
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo2.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
link=task.getResult().getStorage().getDownloadUrl().toString();
Signup();
}
}