3

I want to send Emails using android intent. Every thing is working well except when choosing email app to send email with, in the send to field getting null value, although I check for null values but seems I cannot detect when a string is null. Can anybody help me solve this.

if (emailAddress[0]!=null && !emailAddress[0].isEmpty()) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
                intent.putExtra(Intent.EXTRA_SUBJECT,
                        getResources().getString(R.string.email_sub));
                // intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

                startActivity(Intent.createChooser(intent, "Send Email"));
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • I don't think the `!emailAddress[0].isEmpty()` is necessary... I could be wrong though... – TronicZomB Mar 22 '13 at 12:45
  • 1
    use this .. public static boolean isNotNullNotEmptyNotWhiteSpaceOnlyByJava( final String string) { return string != null && !string.isEmpty() && !string.trim().isEmpty(); } – itsrajesh4uguys Mar 22 '13 at 12:46

6 Answers6

2

Check the String with equals() or equalsingorecase();

String[]  emailAddress = new String[10]; 

          emailAddress[0]="asdfasdfasdfasdf";

          if (emailAddress[0]!=null && !emailAddress[0].isEmpty())
          {
              System.err.println("asddddddd  " +emailAddress[0] );
          }
Siddharth
  • 9,349
  • 16
  • 86
  • 148
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
2

try to use this syntax its detect null and empty string

if(!TextUtils.isEmpty(emailAddress[0]))
steevoo
  • 621
  • 6
  • 18
0

You can use TextUtils.isEmpty(CharSequence str) method to detect if String is empty or null

amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

TextUtils.isEmpty(charSequence char) is used for null String if it don't work then check if(emailAddress != null) because emailAddress[0] position value is null then your array is also null.

Bhargav Panchal
  • 1,159
  • 1
  • 12
  • 27
0

it might help you..

you can use any from below 2

if (emailAddress[0].toString().equals(""))

or

if (emailAddress[0].toString().length() > 0)
Ajay
  • 1,189
  • 1
  • 12
  • 28
0

The string may not be null. AFAIK Intent.EXTRA_EMAIL takes String array as second argument.

Try this.

String[] addressArray = {"add1@mail.com", "a2@mail.com"};

//some lines...

intent.putExtra(Intent.EXTRA_EMAIL, addressArray);
akaya
  • 1,140
  • 9
  • 27