0

In the register page, when a user clicks on the "ImageUserPhoto", he can pick an image from the gallery. My code works fine when he picks the photo and fills up all given fields. But if he doesn't pick any photo, the app crashes. How can I give a warning if he doesn't choose any photo in the same section where I check all the fields? Thanks in advance!!

package com.ayon.austmart.activities;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;


import com.ayon.austmart.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import de.hdodenhof.circleimageview.CircleImageView;


public class RegisterActivity extends AppCompatActivity {

    CircleImageView ImgUserPhoto;
    static int PReqCode = 1;
    static int REQUESCODE = 1;
    Uri pickedImgUri;


    private EditText userEmail, userPassword, userPassword2, userName;
    private ProgressBar loadingProgress;
    private Button regBtn;
    private Intent homeIntent;

    private FirebaseAuth mAuth;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        userEmail = findViewById(R.id.Email);
        userPassword = findViewById(R.id.Password);
        userPassword2 = findViewById(R.id.ConfirmPassword);
        userName = findViewById(R.id.Name);
        loadingProgress = findViewById(R.id.progressBarRegister);
        regBtn = findViewById(R.id.buttonRegister);
        loadingProgress.setVisibility(View.INVISIBLE);

        mAuth = FirebaseAuth.getInstance();

        ImgUserPhoto = findViewById(R.id.avatar);



        regBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regBtn.setVisibility(View.INVISIBLE);
                loadingProgress.setVisibility(View.VISIBLE);
                final String email = userEmail.getText().toString();
                final String password = userPassword.getText().toString();
                final String password2 = userPassword2.getText().toString();
                final String name =userName.getText().toString();

                if(email.isEmpty() || name.isEmpty() || password.isEmpty() || password2.isEmpty() || !password.equals(password2))
                {
                    //something goes wrong... display an error message
                    showMessage("Please verify full fields!!");

                    regBtn.setVisibility(View.VISIBLE);
                    loadingProgress.setVisibility(View.INVISIBLE);




                }

                else
                {


                    //Everything is ok..

                    createUserAccount(email,name,password);


                }
            }
        });



        ImgUserPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(Build.VERSION.SDK_INT >= 22)
                {
                    checkAndRequestForPermission();
                }

                else
                {
                    openGallery();
                }


            }
        });

    }

    private void createUserAccount(String email, final String name, String password) {

        //this method create user account with valid email and pass


        mAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful())
                        {
                            //user account is created successfully
                            showMessage("Account Created!");
                            //now update the pro pic and username

                            updateUserInfo(name,pickedImgUri,mAuth.getCurrentUser());

                        }

                        else
                        {
                            showMessage("Account creation failed"+task.getException().getMessage());
                            regBtn.setVisibility(View.VISIBLE);
                            loadingProgress.setVisibility(View.INVISIBLE);
                        }
                    }
                });





    }


    private void updateUserInfo(final String name, Uri pickedImgUri, final FirebaseUser currentUser){

        StorageReference mStorage = FirebaseStorage.getInstance().getReference().child("user_photos");
        final StorageReference imageFilePath = mStorage.child(pickedImgUri.getLastPathSegment());
        imageFilePath.putFile(pickedImgUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                //image uploaded successfully
                //getting image url

                imageFilePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                       // uri contains user image url
                        UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                                .setDisplayName(name)
                                .setPhotoUri(uri)
                                .build();


                        currentUser.updateProfile(profileUpdate)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {

                                        if(task.isSuccessful())
                                        {
                                            //user info updated successfully

                                            showMessage("Register Complete!");
                                            updateUI();
                                        }

                                    }
                                });


                    }
                });

            }
        });



    }

    private void updateUI() {

          homeIntent = new Intent(getApplicationContext(), Home.class);
    startActivity(homeIntent);
    finish();


    }

    //message show
    private void showMessage(String message) {
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();

    }

    private void openGallery() {

        //Open gallery intent and wait for user to pick an image

        Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
        galleryIntent.setType("image/*");
        startActivityForResult(galleryIntent,REQUESCODE);


    }


    public void checkAndRequestForPermission()
    {
        if(ContextCompat.checkSelfPermission(RegisterActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED)
        {
            if(ActivityCompat.shouldShowRequestPermissionRationale(RegisterActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE))
            {
                Toast.makeText(RegisterActivity.this,"Please accept for required permission",Toast.LENGTH_SHORT ).show();
            }

            else
            {
                ActivityCompat.requestPermissions(RegisterActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PReqCode);
            }


        }

        else
            openGallery();


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK && requestCode == REQUESCODE && data !=null)
        {
            //user has successfully picked an image...
            //saving its reference to a Uri variable

            pickedImgUri = data.getData();
            ImgUserPhoto.setImageURI(pickedImgUri);


        }
    }
}
4
  • Photo shouldn't be a mandatory thing, it should always be optional. Use a placeholder image. if he wants he can change it or just use any generic image you like. And my guess is that you are not initializing the ImageView with placeholder image. share more code regarding the imageView you are using Mar 18, 2019 at 18:34
  • Thank you. I have edited my post with full code. Please check. Mar 18, 2019 at 18:48
  • You know if the user has picked image or not in "onActivityResult" . Based on that you can alert the user.
    – Ravi
    Mar 18, 2019 at 18:49
  • 1
    The problem is in updateUserInfo even if user not select image you are still trying to use pickedImgUri which is null and crashing. Mar 18, 2019 at 18:59

2 Answers 2

1

your pickedImgUri is null if the user does not select the image from gallery.

So when you call createUserAccount() when register button is clicked and user has not selected the image pickedImgUri is not updated as per your code. Hence updateUserInfo() will fail calling an exception which causes the crash.

please add a null checker like

if(pickedImgUri!=null){
    //then update the user account
}

If you are interested in forcing the user to select the profile photo then simply use above checker and alert user to please select a photo first but i'd advice against that. Not everyone likes to upload a photo. use any generic avatar png file instead when user does not want to update/remove the avatar. Happy coding

0

Thanks a lot!! I have added a few lines and it worked!!! Thank you very much!!

 if(email.isEmpty() || name.isEmpty() || password.isEmpty() || password2.isEmpty() || !password.equals(password2))
                {
                    //something goes wrong... display an error message
                    showMessage("Please verify full fields!!");

                    regBtn.setVisibility(View.VISIBLE);
                    loadingProgress.setVisibility(View.INVISIBLE);




                }

                else
                {

                    if(pickedImgUri == null)
                    {

                        showMessage("Please select an image");
                        regBtn.setVisibility(View.VISIBLE);
                        loadingProgress.setVisibility(View.INVISIBLE);

                    }
                    else {
                        createUserAccount(email, name, password);

                        //Everything is ok..
                    }




                }
            }
        });
1
  • Glad to know. You can mark my answer as correct. So that it helps others too Mar 18, 2019 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.