在电子邮件中附加文件

时间:2014-02-04 13:29:39

标签: android

public void onClick(View arg0) {

        String fname1 = text_fname.getText().toString();
        String fname2 = edit_fname.getText().toString();

        String lname1 = text_lname.getText().toString();
        String lname2 = edit_lname.getText().toString();    

        String space = "\t";
        String newLine = "\n";

        File file = null;
        FileOutputStream fos = null;

        try {
            file = getActivity().getFilesDir();
            fos = getActivity().openFileOutput("test.xls", Context.MODE_PRIVATE);
            fos.write(fname1.getBytes());
            fos.write(space.getBytes());
            fos.write(fname2.getBytes());
            fos.write(newLine.getBytes());
            fos.write(lname1.getBytes());
            fos.write(space.getBytes());
            fos.write(lname2.getBytes());
            fos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (fos!=null) {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        Toast.makeText(getActivity(), "File saved in " + file, Toast.LENGTH_LONG).show();

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient@example.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT, "body of email");
        startActivity(Intent.createChooser(i, "Send mail..."));
    }

点击按钮,我正在创建一个带有字符串的“test.xls”文件,并且还调用它:

enter image description here

这是点击gmail后的输出:

enter image description here

我的问题是,如何在我的电子邮件中附加“test.xls”文件?所以我可以把它发送给我喜欢的任何收件人。

4 个答案:

答案 0 :(得分:1)

File file = new File(Environment.getExternalStorageState()+"/folderName/test.xls");
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("application/octet-stream");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        String to[] = { email };
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.putExtra(Intent.EXTRA_STREAM, path);
        startActivityForResult(Intent.createChooser(intent, "Send mail..."),
                1222);

答案 1 :(得分:1)

将以下行添加到Intent

String PATH="Full path of the File that you want to send" ;
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(PATH));

答案 2 :(得分:0)

使用以下代码。

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("application/octet-stream");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
                "mail-id" });
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
        Uri uri = Uri.fromFile(new File(Environment
                .getExternalStorageDirectory(), "/folder_name/file_name"));
        emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
        emailIntent.setType("text/plain");
        startActivity(emailIntent);

答案 3 :(得分:0)

尝试这种方法,在您的项目中创建 EmailActivity.java 类并粘贴此代码..

package com.app.yourpackegename;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

public class EmailActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //  to send data.xls stored in Root of SDCARD 
        // Environment.getExternalStorageState() returns location of sdcard

        String fullFilePath = Environment.getExternalStorageState()+ "/data.xls"; 
        String email = "you@yourdomain,com";
        String subject = "email subject";
        String message = "custome message string";

        File file = new File(fullFilePath);
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("application/octet-stream");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        String to[] = { email };
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.putExtra(Intent.EXTRA_STREAM, path);

        int requestCode = 1000;

        startActivityForResult(
                Intent.createChooser(intent, "Send mail via..."), requestCode);
    }
}

希望这会对你有所帮助。让我知道:))

相关问题