如何将图像和音频文件附加到Android中的电子邮件?

时间:2012-12-01 10:50:34

标签: java android android-layout android-intent

我想将图像和音频文件附加到E-Mail。如何做到这一点。我在SO上找到了很多,但仍然无法得到它的解决方案。我尝试了很多。 请有人帮我解决这个问题。谢谢你。我的代码是:

Intent email = new Intent(Intent.ACTION_SEND_MULTIPLE);
        email.putExtra(Intent.EXTRA_EMAIL, new String[] {});
        // email.setType("image/png");
        email.setType("*/*");
        email.putExtra(Intent.EXTRA_SUBJECT, TAG);
        email.putExtra(Intent.EXTRA_TEXT,
                getResources().getText(R.string.Message));

        ArrayList<Uri> imageUris = new ArrayList<Uri>();

        Uri imageUri1 = Uri.parse("android.resource://" + getPackageName()
                + "/" + R.drawable.ic_launcher);

        // Uri imageUri2 = Uri.parse("android.resource://" +
        // getPackageName()
        // + "/" + R.drawable.twitter);

        Uri imageUri2 = Uri.parse("file:///android_asset/Male_Hard_2.mp3");

        imageUris.add(imageUri1); // Add your image URIs here
        imageUris.add(imageUri2);

        email.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);

        // AssetManager assetManager = getAssets();
        // InputStream inputStream = null;
        // try {
        // inputStream = assetManager.open("Male_Hard_2.mp3");
        // } catch (IOException e) {
        // Log.e("message: ", e.getMessage());
        // }

        // Uri uri = Uri.parse("android.resource://" + getPackageName() +
        // "/"
        // + R.drawable.ic_launcher);
        //
        // Uri uri1 = Uri.parse("file:///android_asset/male_hard_2");
        //
        // email.putExtra(Intent.EXTRA_STREAM, uri);
        //
        // email.putExtra(Intent.EXTRA_STREAM, uri1);

        startActivity(Intent.createChooser(email,
                "Choose an Email client :"));

3 个答案:

答案 0 :(得分:0)

尝试以下代码......

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"email"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Test");
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
    File fileIn = new File(file);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(emailIntent);

答案 1 :(得分:0)

我在MailSenderActivity(m.attachment())中附加了sdcard附件图像的示例代码..这个

<强> MailSenderActivity.java

公共类MailSenderActivity扩展了Activity {

private static final String GMAIL_EMAIL_ID = "";
private static final String GMAIL_ACCOUNT_PASSWORD = "";
private static final String TO_ADDRESSES = "";

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

MailSender.execute();

}

class MailSender extends AsyncTask<Void, Integer, Integer> {

    ProgressDialog pd = null;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(MailSenderActivity.this);
        pd.setTitle("Sending...");
        pd.setMessage("Mail Sending. Please wait...");
        pd.setCancelable(false);
        pd.show();

    }


    @Override
    protected Integer doInBackground(Void... params) {


        Mail m = new Mail(GMAIL_EMAIL_ID, GMAIL_ACCOUNT_PASSWORD);

        String toAddresses = TO_ADDRESSES;
        m.setToAddresses(toAddresses);
        m.setFromAddress(GMAIL_EMAIL_ID);
        m.setMailSubject("captured image.");
        m.setMailBody("Email body.");

        try {

             m.addAttachment("/mnt/sdcard/cpimg.jpg");

            if (m.send()) {
                System.out.println("Message sent");
                return 1;
            } else {
                return 2;
            }

        } catch (Exception e) {
            Log.e("MailApp", "Could not send email", e);
        }
        return 3;
    }


    @Override
    protected void onPostExecute(Integer result) {

        super.onPostExecute(result);
        pd.dismiss();

        if (result == 1)
            Toast.makeText(MailSenderActivity.this,
                    "Email was sent successfully.", Toast.LENGTH_LONG)
                    .show();
        else if (result == 2)
            Toast.makeText(MailSenderActivity.this, "Email was not sent.",
                    Toast.LENGTH_LONG).show();
        else if (result == 3)
            Toast.makeText(MailSenderActivity.this,
                    "There was a problem sending the email.",
                    Toast.LENGTH_LONG).show();

    }
}

}

希望这会有所帮助..

答案 2 :(得分:0)

尝试使用音频文件:

Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("audio/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL, new String[] {"someone@gmail.com"} );
    i.putExtra(Intent.EXTRA_SUBJECT, "MySubject");
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "audiorecorder.3gpp");
    startActivity(i)
相关问题