Tom Walford
Tom Walford

Reputation: 11

Adding an attachment to an email (Android SDK)

Before proceeding, it's important for you to realise that this question really comes from one of my students. He has to produce an app (using Android SDK) as part of a school project. Neither he, nor I, have a fantastic working knowledge of Java, which is the required language.

What we have thus far is as follows:

package com.example.thomas.sendemailtaketwenty;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    Button sendEmail;
    EditText msg;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sendEmail = (Button) findViewById(R.id.sendBtn);
        sendEmail.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                msg = (EditText) findViewById(R.id.msgTxt);
                String message = msg.getText().toString();
                sendEmail(message);


            }
        });
    }

    protected void sendEmail(String message) {
        File externalStorage = Environment.getExternalStorageDirectory();

        Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "103950019.jpg"));


        String[] to=new String[]{"[email protected]"};
        String subject = ("House Rosebowl Scores");
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
        emailIntent.putExtra(Intent.EXTRA_TEXT, message);
        emailIntent.setType("message/rfc822");
        startActivity(Intent.createChooser(emailIntent, "Email"));

    }

}

It will load the email program (have tried Outlook and Gmail clients) but they will not load the attachment (cannot load empty file or io error), which is stored in the root of the external SD Card.

Please can anyone help, but equally, please remember that we need "baby steps" not "giant leaps" in terms of help!

Many thanks.

Tom.

Upvotes: 1

Views: 115

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007658

Step #1: Replace

Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "103950019.jpg"));

with

Uri uri = Uri.fromFile(new File(externalStorage, "103950019.jpg"));

as this is more resilient to the particulars of how the external storage root directory is encoded.

Step #2: Remove Intent.EXTRA_TEXT. The ACTION_SEND documentation says that either EXTRA_TEXT or EXTRA_STREAM is supported. Using both may cause problems.

Step #3: Replace

emailIntent.setType("message/rfc822");

with

emailIntent.setType("image/jpeg");

as unless you have a very strange attachment, that attachment is a JPEG file, not an RFC822 message.

Step #4: Consider using ACTION_SENDTO and a mailto: Uri, instead of ACTION_SEND and EXTRA_EMAIL, as Step #3 will cause your request to match non-email-related applications.

Step #5: Make sure that you have run Outlook and/or Gmail, have set up accounts, and (if you are on Android 6.0+) have granted them permission to work with external storage. The latter is particularly important, as your code may be perfectly fine, but if the other app cannot access external storage, your attachment will still not work. As a workaround for this, you might use FileProvider to serve a file from your app's internal storage, though that comes with its own set of compatibility issues.

Upvotes: 2

Related Questions