How to Validate ImageView where we are getting image from gallery

Viewed 66

I am sending getting an image from gallery in imageview and want to send this imageview along with edittext but when I left imageview empty and press the update button, the app crashes. How to validate my image view along with edittext android Kotlin here I have icons on my actionbar one is an update and the other is edit when I press edit my edittext becomes editable but when press update I have validation on edittext which will show "please enter.." but when I keep edittext unchanged and try to update my imageview cause problem and app crash

R.id.update_icon -> {


           email.setFocusable(false);
            name.setFocusable(false);
            phone.setFocusable(false);
            cameraIcon.setVisibility(View.GONE);


       if(TextUtils.isEmpty(name.text.toString()) && TextUtils.isEmpty(phone.text.toString())){
                name.error="Enter user name"
                phone.error="Enter Phone number"

        }
            else if(ImageUri==null || imageView.equals(null) || imageView.visibility==null|| imageView.drawable==null){

           imageView.setEnabled(false);
           Toast.makeText(this, "you have not choosed any profile picture", Toast.LENGTH_SHORT).show()

       }



            else{

                val formater = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.getDefault())
                val now = Date()
                val filename = formater.format(now)
                val storage = FirebaseStorage.getInstance().getReference("images/$filename")

                storage.putFile(ImageUri).addOnSuccessListener {

                    imageView.setImageURI(null)
                    Toast.makeText(this, "Sucessfully added data", Toast.LENGTH_SHORT).show()

                }.addOnFailureListener() {
                    Toast.makeText(this, "Image uploading failed", Toast.LENGTH_SHORT).show()
                }


                Toast.makeText(this, "you have not choosed any profile picture", Toast.LENGTH_SHORT).show()

                database = FirebaseDatabase.getInstance().getReference("Users")

                val user = mapOf<String, String>(
                    "username" to name.text.toString(),
                    "phoennumber" to phone.text.toString()
                )

                database.child(name.text.toString()).updateChildren(user).addOnSuccessListener {

                    Toast.makeText(applicationContext, "Sucessfully Updated ", Toast.LENGTH_LONG)
                        .show()

                }.addOnFailureListener() {
                    Toast.makeText(applicationContext, "Failed", Toast.LENGTH_LONG).show()
                }



            }


            return true

        }




        else -> onOptionsItemSelected(item)

}
1 Answers

In your code, you're checking
if(imageView == null) and then you're accessing it. Which may give a NullPointerException.

Related