and-nd-firebase icon indicating copy to clipboard operation
and-nd-firebase copied to clipboard

unable to upload photo

Open Shayan-Aslam opened this issue 5 years ago • 3 comments

I can select photo but could not upload the photo to firebase

Shayan-Aslam avatar Apr 30 '19 12:04 Shayan-Aslam

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;
    }

}

cooldip00 avatar Jun 22 '19 15:06 cooldip00

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.

sushen avatar Oct 29 '19 13:10 sushen

I can select photo but could not upload the photo to firebase

Below solution work for me.

sushen avatar Oct 29 '19 13:10 sushen