2

I have sent email via ACTION_SEND in android.

Here its successfully sent from android 2.2,but it is didn't sent from android 4.1.

I have used below code:

  ImageView share = (ImageView) findViewById(R.id.imageView5);
  share.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    final CharSequence[] items = {"Facebook", "Twitter", "Email"};

  AlertDialog.Builder builder = new AlertDialog.Builder(SubCate.this);
  builder.setTitle("Share Via:");
  builder.setItems(items, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int item) {
   if(items[item] == "Facebook"){
        Intent in = new Intent(SubCate.this, TestConnect.class);
        in.putExtra("Title", _Substring); 
    startActivity(in);    
    } 
  if(items[item] == "Twitter"){
        Intent in = new Intent(SubCate.this, TestConnect.class);
        in.putExtra("Title", _Substring); 
     startActivity(in);    
   } 
   if(items[item] == "Email"){
           Intent email = new Intent(Intent.ACTION_SEND);
           email.setType("message/rfc822");           
           email.putExtra(Intent.EXTRA_EMAIL, new String[]{ ""});           
           email.putExtra(Intent.EXTRA_SUBJECT, _Substring);
           email.putExtra(Intent.EXTRA_TEXT, _Description);
           email.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));         
           startActivity(Intent.createChooser(email, "Choose an Email client :"));

         } 
          }
          }); 
        AlertDialog alert = builder.create();
  alert.show();
      }  
 }); 

Why these code is didn't work in android 4.0 device.pls give me solution for these..

Here I have using alertlist dialog box.

I didn't use surface view anywhere.if i have remove these line

email.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));

means its worked... the subject and text is sent to mail from android4.0...but i ahve facing problem only adding attach the image...pls give me solution.

Am getting following error:

        04-12 10:36:04.722: E/ViewRootImpl(1461): IllegalArgumentException locking surface
        04-12 10:36:04.722: E/ViewRootImpl(1461): java.lang.IllegalArgumentException
        04-12 10:36:04.722: E/ViewRootImpl(1461):   at android.view.Surface.lockCanvasNative(Native Method)
        04-12 10:36:04.722: E/ViewRootImpl(1461):   at android.view.Surface.lockCanvas(Surface.java:76)
        04-12 10:36:04.722: E/ViewRootImpl(1461):   at android.view.ViewRootImpl.draw(ViewRootImpl.java:1924)
        04-12 10:36:04.722: E/ViewRootImpl(1461):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1613)
        04-12 10:36:04.722: E/ViewRootImpl(1461):   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at android.os.Handler.dispatchMessage(Handler.java:99)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at android.os.Looper.loop(Looper.java:137)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at android.app.ActivityThread.main(ActivityThread.java:4340)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at java.lang.reflect.Method.invokeNative(Native Method)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at java.lang.reflect.Method.invoke(Method.java:511)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
         04-12 10:36:04.722: E/ViewRootImpl(1461):  at dalvik.system.NativeStart.main(Native Method)

EDIT:

    ImageView share = (ImageView) getActivity().findViewById(R.id.imageView5);
    share.setOnClickListener(new OnClickListener()
      {
          public void onClick ( View v )
          {

            final CharSequence[] items =
            {
                    "Facebook", "Twitter", "Email"
              };

           AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              builder.setTitle("Share Via:");
              builder.setItems(items, new DialogInterface.OnClickListener()
             {
                public void onClick ( DialogInterface dialog , int item )
                {

                    if (items[item] == "Facebook")
                    {

                        onFacebookClick();
                    }
                    if (items[item] == "Twitter")
                    {

                        onClickTwitt();
                    }
                    if (items[item] == "Email")
                    {
                        class LongOperation extends AsyncTask<String, Void, String> {

                              protected String doInBackground(String... params) {

                                   Intent email = new Intent(Intent.ACTION_SEND);
                                   email.setType("message/rfc822");           

                        email.putExtra(Intent.EXTRA_EMAIL, new String[]
                        {
                                ""
                        });
                        email.putExtra(Intent.EXTRA_SUBJECT, _Title);


                        email.putExtra(Intent.EXTRA_STREAM, Uri.parse(_Image));  
                        startActivity(Intent.createChooser(email, "Choose an Email client :"));


                                return "Executed";
                          }      

                          protected void onPostExecute(String result) {
                                       }

                          protected void onPreExecute() {
                          }

                          protected void onProgressUpdate(Void... values) {
                          }

                        }
                        }
                }
            });

              AlertDialog alert = builder.create();
              alert.setCanceledOnTouchOutside(true);
              alert.show();
                  }
        });
    }

I have to run the app and click email means i didn't get anything...If i ahve to click facebook means facebook login is opening.But click email means i didn't get email compose message and didn't getting any error ??? Why ??? what's wrong in my code ...

user2218667
  • 607
  • 6
  • 25
  • 46
  • Try out by writing your package name like `"android.resource://your.package.name/"` besides `getPackageName()` method. – GrIsHu Apr 12 '13 at 05:19
  • @Grishu i have changed my code like :email.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ "com.xmlparsing" + "/" + R.drawable.ic_launcher)); but now also am getting same error only... – user2218667 Apr 12 '13 at 05:23
  • Try to take another image besides launcher image. – GrIsHu Apr 12 '13 at 05:29
  • @Grishu hi i have used another image also.now also getting same error only...also this is worked on android 2.2 ...didn't working on android 4.0 ly...in android 4.0 have sent EXTRA_SUBJECT, EXTRA_TEXT....i have added EXTRA_STREAM means ly getting error... – user2218667 Apr 12 '13 at 05:36
  • I think you are using SurfaceView in your code somewhere. Can you post some more code ? – GrIsHu Apr 12 '13 at 05:40
  • @Grishu i have added my code above...pls check it.here i didn't use surface view anywhere... – user2218667 Apr 12 '13 at 06:08
  • Have tried changing the `email.setType("image/png");` ? – GrIsHu Apr 12 '13 at 06:26
  • Why you didn't specified any email id for sending the mail in `EXTRA_EMAIL` line . Please try to provide email address. – GrIsHu Apr 12 '13 at 06:28
  • i have changed email.setType("image/png"); like above..also provided email id on that EXTRA_EMAIL...now also getting same error yar... – user2218667 Apr 12 '13 at 06:47
  • Post your whole activity code please if possible . – GrIsHu Apr 12 '13 at 07:37
  • Check out my updated answer and try that way. – GrIsHu Apr 12 '13 at 07:47

4 Answers4

0

This can be due to the reason that you cannot do network operation from the any UI activity in newer versions of android. Folow this link to make it work.

private class LongOperation extends AsyncTask<String, Void, String> {

  @Override
  protected String doInBackground(String... params) {

       Intent email = new Intent(Intent.ACTION_SEND);
       email.setType("message/rfc822");           
       email.putExtra(Intent.EXTRA_EMAIL, new String[]{ ""});           
       email.putExtra(Intent.EXTRA_SUBJECT, _Substring);
       email.putExtra(Intent.EXTRA_TEXT, _Description);
       email.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));         
       startActivity(Intent.createChooser(email, "Choose an Email client :"));

  }

        return "Executed";
  }      

  @Override
  protected void onPostExecute(String result) {
               }

  @Override
  protected void onPreExecute() {
  }

  @Override
  protected void onProgressUpdate(Void... values) {
  }
}   
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Try to declare your String array as a global variable before `onCreate()` method. `final CharSequence[] items = {"Facebook", "Twitter", "Email"};` – GrIsHu Apr 12 '13 at 08:53
  • @GrlsHu please check my updated question and Give me solution – user2218667 May 17 '13 at 13:31
0

You use droid share for sending email.in droid share main part is .setType(for email you use email/plain). Try this, I'm sure its works.

// TODO Auto-generated method stub

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("email/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "SUBJECT_TEXT");
emailIntent.putExtra(Intent.EXTRA_TEXT, "EXTRA_TEXT");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));
startActivity(Intent.createChooser(emailIntent, "Send email using"));
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • you try to resolve your prob maybe from this link you get some idea. https://code.google.com/p/android/issues/detail?id=22406 one test example also are there so, you its easy to understood for you. – Mr.Sandy Apr 12 '13 at 05:46
0

you have to copy your resource file in to sdcard after that attach in Email it work

check this and this link for more detail.

copy resource to sdcard android

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

Try to use below code on your Email item click.MAy it help you.Tt will send both image as well as text.You can send data as you want..

Bitmap bmp_cmpressed = Bitmap.createScaledBitmap(edited_image, 60,
                60, false);
        File file_image = saveBitmap(bmp_cmpressed);
        Uri uri = Uri.fromFile(file_image);
        Intent mail_image = new Intent(android.content.Intent.ACTION_SEND);
        mail_image.setType("plain/text");
        mail_image.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] {});
        mail_image.putExtra(android.content.Intent.EXTRA_SUBJECT, "Image");
        mail_image.putExtra(android.content.Intent.EXTRA_STREAM, uri);
        mail_image.putExtra(android.content.Intent.EXTRA_TEXT, "Hello...");
        Intent intent_mail = Intent.createChooser(mail_image, "Send Mail");
        startActivity(intent_mail);
AndiM
  • 2,196
  • 2
  • 21
  • 38