I need to get the image URL after I uploaded it to firebase storage. After that, the image URL will be added to my firebase database together with other user data. However, the link keeps perceived as abc, I wonder why it will not be overwritten by the
link = task.getResult().getStorage().getDownloadUrl().toString();
In the uploadImage()
method. Can someone help me?
Thanks in advance :)
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);
}
}
It seems like your Signup() method fired before the asynchronous task running in uploadImage(). Try to put Signup() into onComplete() in uploadImage().
Such like,
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();
}
}