Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to upload photo #62

Open
Shayan-Aslam opened this issue Apr 30, 2019 · 3 comments
Open

unable to upload photo #62

Shayan-Aslam opened this issue Apr 30, 2019 · 3 comments

Comments

@Shayan-Aslam
Copy link

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

@cooldip00
Copy link

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

}

@sushen
Copy link

sushen commented Oct 29, 2019

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
Copy link

sushen commented Oct 29, 2019

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

Below solution work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants