Reputation: 11
I am uploading two "user-selected" images to firebase storage and getting their URL to save to the firestore, but the URLs save point to only one of the pictures although they don't appear to be the same. there is a similar question but about swift and it doesn't answer my question, here is the URL to that. get download url from multiple file upload firebase storage
I tried getting the download links with ref.getDownloadUrl() but that gave wrong URLs and tasksnapshot.getDownloadUrl() doesn't work anymore. I tried reading lots of answers but nothing helped most are outdated with tasksnapshot.getDownloadUrl() method.
if (profileImageUri != null) {
//upload first image
profileImageRef.putFile(profileImageUri).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();
}
return profileImageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
profileImageUrl = String.valueOf(downloadUri);
saveUserInfo(); // method to save the URLs along with other info
} else {
Toast.makeText(EditProfile.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
btSaveInfo.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
});
}
if (coverImageUri != null) {
// Upload second image
profileImageRef.putFile(coverImageUri).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();
}
return profileImageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadCUri = task.getResult();
coverImageUrl = String.valueOf(downloadCUri);
Toast.makeText(getApplicationContext(), "Cover Picture Uploaded", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(EditProfile.this, "Cover picture upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
the two images are uploaded I can see them in my firebase console but the URLs saved in firestore both load the same image although they are not the same, they look like this,
how do I get the two URLs that will load the two pictures that I uploaded not only one
thanks for your help, much appreciated.
Upvotes: 0
Views: 1444
Reputation: 11
I just realized that I wasn't saving my info after uploading the cover image, I added the saveInfo method in my onComplete of cover image upload method also and it works fine now.
final StorageReference profileImageRef = FirebaseStorage.getInstance()
.getReference(SefnetContract.PROFILE_PICS_REF + System.currentTimeMillis() + ".jpg");
// Upload profile picture
if (profileImageUri != null) {
progressBar.setVisibility(View.VISIBLE);
profileImageRef.putFile(profileImageUri).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();
}
return profileImageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
profileImageUrl = String.valueOf(downloadUri);
saveUserInfo();
} else {
Toast.makeText(EditProfile.this, "upload failed: "
+ task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
progressBar.setVisibility(View.GONE);
}
});
}
if (coverImageUri != null) {
// Upload cover picture
progressBar.setVisibility(View.VISIBLE);
profileImageRef.putFile(coverImageUri).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();
}
return profileImageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
Uri downloadCUri = task.getResult();
coverImageUrl = String.valueOf(downloadCUri);
saveUserInfo();
Toast.makeText(getApplicationContext(), "Cover Picture Uploaded", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(EditProfile.this, "Cover picture upload failed: "
+ task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
I still feel like it could be done in a better way so if you know of a method better than this please share. I would like a method in which I can stop the user from initializing the upload method too many times by clicking the button repeatedly, thank you
Upvotes: 0