【问题标题】:How to attach Image and Audio file to E-mail in android?如何在 android 中将图像和音频文件附加到电子邮件?
【发布时间】:2012-11-19 11:13:45
【问题描述】:

我想将图像和音频文件附加到电子邮件。如何做到这一点。我在 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 :"));

【问题讨论】:

  • 你只能发送SD卡上的文件(不是APK资源)stackoverflow.com/questions/10946203/…
  • 实际上我得到了图像,但我不知道如何将我的资产文件夹中的音频文件附加到电子邮件。
  • 不,你错了。只要确定你真的能够从资源中发送图像吗?检查接收方。我确定您需要将文件放入 Sdcard 中,然后就可以了。
  • 是的,我通过应用 setType("image/png") 在 Receiver 端获得了图像。这没有问题。但我不知道如何从 assets 文件夹中附加音频文件。跨度>
  • 你能在接收端打开图片吗?

标签: java android android-layout android-intent


【解决方案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)

【讨论】:

    【解决方案2】:

    我已在 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();
    
        }
    }
    

    }

    希望这会有所帮助..

    【讨论】:

    • 你知道如何使用 Intent 将音频文件从 assets 文件夹附加到电子邮件吗?
    【解决方案3】:

    试试下面的代码...

    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);
    

    【讨论】:

    • 我想将资产文件夹中的音频文件附加到电子邮件中。我不想从 SD 卡中取出它。
    • 所以把你的文件路径放在for循环中的filePaths
    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    相关资源
    最近更新 更多