kevskree
kevskree

Reputation: 4602

Sending email attachment android with intent. Only works in Gmail

I am trying to send a .png file with an Android intent. I have tried saving this file in internal storage with WorldReadable permissions and now have saved to the external storage. When I open the GMail client my attachment is there. However, in Microsoft Exchange or Outlook app the attachment does not appear and I have to add it manually.

I'm using Xamarin.Android (MonoDroid) but any java solutions would be helpful as well. Here is my code for sending the email with the intent.

        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setType("image/png"); // I also tried 'application/image'
        intent.setData(Android.Net.Uri.Parse("mailto:" + address ?? string.Empty));

        if (!string.isNullOrEmpty (attachment)) {
            intent.putExtra (Android.Content.Intent.EXTRA_STREAM, Android.Net.Uri.fromFile(new Java.IO.File (Android.OS.Environment.getExternalStorageDirectory(), attachment)));
        }

        try {
            _Context.startActivity(intent);
        }catch(ActivityNotFoundException anfe) {

            //Show prompt
        }

I'm not sure why the attachment only shows up in GMail. Do I need a content provider? It's weird that its only showing up in GMail and not any other mail applications.

Upvotes: 3

Views: 5983

Answers (3)

Michele Caggiano
Michele Caggiano

Reputation: 325

Try to use ActionSendMultiple Intent: Intent(Intent.ActionSendMultiple).

For attachments use PutParcelableArrayListExtra(Intent.ExtraStream, uris) where uris variable is a List<IParcelable>().

Here's an example:

var email = new Intent(Intent.ActionSendMultiple);
    email.SetType("text/plain");
    email.PutExtra(Intent.ExtraEmail, new string[]{emailTo});
    email.PutExtra(Intent.ExtraCc, new string[]{emailCC});

    var uris = new List<IParcelable>();
    filePaths.ForEach(file=> {
        var fileIn = new File(file);
        var uri = Android.Net.Uri.FromFile(fileIn);
        uris.Add(uri);
    });

    email.PutParcelableArrayListExtra(Intent.ExtraStream, uris);

    context.StartActivity(Intent.CreateChooser(email, "Send mail..."));

Hope this helps ;)

Upvotes: 0

user2239842
user2239842

Reputation: 86

    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
    {
        bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.cstand);
        File sdCard = Environment.getExternalStorageDirectory();
        System.out.println("PATH : "+sdCard);
        File dir = new File(sdCard.getAbsolutePath() + "/Salary/Documents/Email");//#2

        dir.mkdirs();
        File file = new File(dir, "Document.png");

        try 
        {
            output = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
            output.flush();
            output.close();
            String subject = "Document" ;
            String message = "Please, see the attcahed document";

            //FOR EMAIL
            Intent email = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null));
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            Uri uri = Uri.fromFile(file);
            email.putExtra(Intent.EXTRA_STREAM, uri);
            email.putExtra(Intent.EXTRA_TEXT, message);
            startActivity(Intent.createChooser(email, "Choose any Email:"));

            }
        catch (android.content.ActivityNotFoundException ex)
        {
            Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        } 
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

Tom Opgenorth
Tom Opgenorth

Reputation: 1511

You shouldn't need a ContentProvider.

I'm doing the basically the same thing for sending a JPEG, but using Intent.ActionSend instead of Intent.ActionSendTo.

Upvotes: 2

Related Questions