2012-12-01 110 views
0

我想附加圖像和音頻文件到電子郵件。如何做到這一點。我在這裏發現了很多,但仍然無法得到它的解決方案。我試了很多。 請人幫助我爲這個issue.Thanks在Advance.My代碼:如何將圖像和音頻文件附加到Android的電子郵件?

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

只能發送文件,該文件是在SD卡(不APK資源)的HTTP ://stackoverflow.com/questions/10946203/android-mime-type-for-email-attachment-not-set/13584068#13584068 –

+0

其實我得到的圖像,但我不知道如何從我的資產附加音頻文件電子郵件文件夾。 – Biginner

+0

不,你錯了。只要確定你真的能夠從資源發送圖像?檢查收貨方。我相信你需要把文件放在SD卡上,然後你就可以。 –

回答

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

我想從資產文件夾附加音頻文件到電子郵件。我不想從SD卡。 – Biginner

+0

所以把你的文件路徑在filePaths for循環 –

0

我重視從SD卡中attachement圖像的示例代碼MailSenderActivity(m.attachment())..try這個

MailSenderActivity.java

公共CL MailSenderActivity擴展活動{

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

    } 
} 

}

希望這將有助於..

+0

你知道如何使用Intent從音頻文件夾中將音頻文件連接到電子郵件嗎? – Biginner

0

試試這一個音頻文件:

Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("audio/rfc822"); 
    i.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
    i.putExtra(Intent.EXTRA_SUBJECT, "MySubject"); 
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "audiorecorder.3gpp"); 
    startActivity(i) 
相關問題