0

我想将图像和音频文件附加到电子邮件。如何做到这一点。我在这里找到了很多,但仍然无法得到解决方案。我尝试了很多。请有人帮我解决这个问题。在此先感谢。我的代码是:

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 :"));
4

3 回答 3

1

试试这个音频文件:

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)
于 2012-12-05T13:42:55.930 回答
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();

    }
}

}

希望这会有所帮助..

于 2012-12-01T12:19:44.423 回答
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);
于 2012-12-01T10:59:31.303 回答