I am working on a simple app that should work like uber, I want each driver to upload the image of his/her car while setting up his/her profile. I have successfully picked the two images and was able to save them to the firebaseStorage, I want to retrieve the link to the downloaded car Image and save it to the driver database reference along with his profile picture and other information but I am unable to do that. I create a method that returns a String so that I can use the String returned by that method as the link to the car Image but it's showing null in my database.
The method that should return the link to the downloaded car image:
private Uri profileImageUri, carImageUri;
private StorageReference storageProfilePicsRef, storageCarImageRef;
private StorageTask uploadProfileTask, uploadCarImageTask;
private String myProfileImaeUri = "", myCarImageUri = "";
private String uploadCarImage() {
if (carImageUri != null) {
final StorageReference storageReference = storageCarImageRef.child(mAuth.getCurrentUser().getUid() + ".jpg");
uploadCarImageTask = storageReference.putFile(carImageUri);
uploadCarImageTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadCarUri = task.getResult();
myCarImageUri = downloadCarUri.toString();
}
}
});
} else {
Toast.makeText(SettingsActivity.this, "Car Image Not Selected", Toast.LENGTH_SHORT).show();
}
return myCarImageUri;
}
This is the method that upload both the profile image and other information of the driver:
private void uploadProfilePicture() {
String carImageUriFromTheMethod = uploadCarImage();//I was trying to assigned the method that returns the carImage link as a String to this String type variable so that I can use it
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Updating Profile");
progressDialog.setMessage("Please wait while we are updating your account information");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
if (profileImageUri != null) {
final StorageReference fileRef = storageProfilePicsRef.child(mAuth.getCurrentUser().getUid() + ".jpg");
uploadProfileTask = fileRef.putFile(profileImageUri);
uploadProfileTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return fileRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
myProfileImaeUri = downloadUri.toString();
int selectedID = radioGroup.getCheckedRadioButtonId();
male = (RadioButton)findViewById(selectedID);
female = (RadioButton) findViewById(selectedID);
if (male.isChecked()){
selectedGender = male.getText().toString();
}else if (female.isChecked()){
selectedGender = female.getText().toString();
}else {
selectedGender = "Not specify";
}
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("ProfileImage", myProfileImaeUri);
hashMap.put("name", name.getText().toString());
hashMap.put("city", city.getText().toString());
hashMap.put("age", age.getText().toString());
hashMap.put("maritalStatus", maritalStatus.getText().toString());
hashMap.put("phoneNumber", phoneNumber.getText().toString());
hashMap.put("country", country.getText().toString());
hashMap.put("address", address.getText().toString());
hashMap.put("gender", selectedGender);
hashMap.put("carName",carName.getText().toString());
hashMap.put("carColour", carColor.getText().toString());
hashMap.put("carPlateNumber", carPlateNumber.getText().toString());
hashMap.put("carImage", carImageUriFromTheMethod);//I now call the string here
}
databaseReference.child(mAuth.getCurrentUser().getUid()).updateChildren(hashMap);
progressDialog.dismiss();
if (getType.equalsIgnoreCase("Driver")) {
startActivity(new Intent(SettingsActivity.this, DriversMapsActivity.class));
Toast.makeText(SettingsActivity.this, "Profile Information Updated Successfully", Toast.LENGTH_SHORT).show();
finish();
} else {
startActivity(new Intent(SettingsActivity.this, CustomerMapsActivity.class));
Toast.makeText(SettingsActivity.this, "Profile Information Updated Successfully", Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(SettingsActivity.this, "Error Occured", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
} else {
Toast.makeText(SettingsActivity.this, "Profile Image Not Selected", Toast.LENGTH_SHORT).show();
}
}
Here is my database which shows that the carImage is null, but there is a car Image in the firebaseStorage:
"Driver": {
"2oKrtyUa3FX1wyHAhplsxbVaPZU2": {
"ProfileImage": "https://firebasestorage.googleapis.com/v0/b/ride-booking-app-e8a95.appspot.com/o/Profile%20Images%2F2oKrtyUa3FX1wyHAhplsxbVaPZU2.jpg?alt=media&token=442b14b3-ffb8-4356-b0c0-4c77021fe391",
"address": "Ikot Obong",
"age": "28",
"carColour": "black",
"carImage": "",//this is where the link supposed to appear as it's seen in the profile image
"carName": "Range Rover sport",
"carPlateNumber": "aa377-ktm",
"city": "Ikot Abasi",
"country": "Nigeria",
"gender": "Male",
"maritalStatus": "single",
"name": "Captain Elijah",
"phoneNumber": "08168989070"
}
}
Thanks for help