0

I need to send an attachment along with some basic information by mail using my app. But the attachment is blank when I'm trying to invoke the mail activity using intent.

Here's the code for getting filepath and then sending the attachment

public class Pickafile extends AppCompatActivity {
TextView textFile;
static String FilePath;
private static final int PICKFILE_RESULT_CODE = 1;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    Button buttonPick = (Button) findViewById(R.id.buttonpick);
    Button bp = (Button) findViewById(R.id.proceed);
    textFile = (TextView) findViewById(R.id.textfile);

    buttonPick.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("file/*");
            startActivityForResult(intent, PICKFILE_RESULT_CODE);

        }
    });

    bp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            //intent.setType("text/plain");
            intent.setData(Uri.parse("mailto:"));
            intent.putExtra(Intent.EXTRA_SUBJECT, " Report Particulars for " + MainActivity.name);
            /*File root = Environment.getExternalStorageDirectory();
            File file = new File(root, FilePath);
            if (!file.exists() || !file.canRead()) {
                return;
            }*/
            /*if(FilePath!=null)
            {
                intent.putExtra(Intent.EXTRA_STREAM, FilePath);
            }*/
            intent.putExtra(Intent.EXTRA_TEXT, MainActivity.testfunc());
            intent.putExtra(Intent.EXTRA_STREAM, FilePath);
            //startActivity(Intent.createChooser(intent, "Pick an Email provider"));
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    switch (requestCode) {
        case PICKFILE_RESULT_CODE:
            if (resultCode == RESULT_OK) {
                FilePath = data.getData().getPath();
                textFile.setText(FilePath);
            }
            break;

    }
}
}

testfunc() is used to get body of the mail (name, age, phone,etc.)

Thanks in advance!

sree
  • 31
  • 5
  • `FilePath = data.getData().getPath(); `. You threw away the content scheme and hence the path. Change to `FilePath = data.getData().toString();` I think ther are more issues. – blackapps Oct 20 '19 at 08:17

1 Answers1

0

You should check the log if there is any error. Possible case: 1. The file is too large, exceed maximum size of email attachment (20Mb for Gmail) 2. Missing READ_EXTERNAL_STORAGE permission if the file is saved in external storage. 3. Missing file_provider in manifest

Duy Khanh Nguyen
  • 454
  • 3
  • 11