and-nd-firebase
and-nd-firebase copied to clipboard
unable to upload photo
I can select photo but could not upload the photo to firebase
use switch case instead of if else condition
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case RC_SIGN_IN:
if (resultCode == RESULT_OK){
Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show();
}else if (resultCode == RESULT_CANCELED){
Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show();
finish();
}
break;
case RC_PHOTO_PICKER:
if (resultCode == RESULT_OK){
Toast.makeText(this, "picker in!", Toast.LENGTH_SHORT).show();
Uri selectedImageUri = data.getData();
// Get a reference to store file at chat_photos/<FILENAME>
final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());
// Upload file to Firebase Storage
photoRef.putFile(selectedImageUri)
.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 photoRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
FriendlyMessage friendlyMessage= new FriendlyMessage(null,mUsername,downloadUri.toString());
mMessagesDatabaseReference.push().setValue(friendlyMessage);
} else {
// Handle failures
// ...
}
}
});
}
break;
default:
Toast.makeText(this, "Something Wrong", Toast.LENGTH_SHORT).show();
break;
}
}
use switch case instead of if else condition
@override protected void onActivityResult(int requestCode, int resultCode, @nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){ case RC_SIGN_IN: if (resultCode == RESULT_OK){ Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show(); }else if (resultCode == RESULT_CANCELED){ Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show(); finish(); } break; case RC_PHOTO_PICKER: if (resultCode == RESULT_OK){ Toast.makeText(this, "picker in!", Toast.LENGTH_SHORT).show(); Uri selectedImageUri = data.getData(); // Get a reference to store file at chat_photos/<FILENAME> final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); // Upload file to Firebase Storage photoRef.putFile(selectedImageUri) .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 photoRef.getDownloadUrl(); } }).addOnCompleteListener(new OnCompleteListener<Uri>() { @Override public void onComplete(@NonNull Task<Uri> task) { if (task.isSuccessful()) { Uri downloadUri = task.getResult(); FriendlyMessage friendlyMessage= new FriendlyMessage(null,mUsername,downloadUri.toString()); mMessagesDatabaseReference.push().setValue(friendlyMessage); } else { // Handle failures // ... } } }); } break; default: Toast.makeText(this, "Something Wrong", Toast.LENGTH_SHORT).show(); break; }
}
Thanks Dear Your Solutions Work. I was almost frustrated to find a perfect solutions.
I can select photo but could not upload the photo to firebase
Below solution work for me.