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"));
2
  • I don't think the !emailAddress[0].isEmpty() is necessary... I could be wrong though... Commented Mar 22, 2013 at 12:45
  • 1
    use this .. public static boolean isNotNullNotEmptyNotWhiteSpaceOnlyByJava( final String string) { return string != null && !string.isEmpty() && !string.trim().isEmpty(); } Commented Mar 22, 2013 at 12:46

6 Answers 6

2

try to use this syntax its detect null and empty string

if(!TextUtils.isEmpty(emailAddress[0]))
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

0

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

Comments

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.

Comments

0

it might help you..

you can use any from below 2

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

or

if (emailAddress[0].toString().length() > 0)

1 Comment

.toString() was the part that was making all the problems.. thanks
0

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

Try this.

String[] addressArray = {"[email protected]", "[email protected]"};

//some lines...

intent.putExtra(Intent.EXTRA_EMAIL, addressArray);

2 Comments

The OP is passing a String[] as the second parameter.
@jprofit My bad. I guess I expected to see emailAddresses or emailAdressArray

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.