2012-01-27 111 views
1

我試圖以編程方式將圖像附加到電子郵件正文中。我看到了一些關於如何做到這一點的主題,並將我的代碼完全一樣,但沒用沒有在另一面得到圖像(從這post)。 更多信息,這裏是我的代碼:未收到附加在電子郵件中的圖像

Intent emailIntent=new Intent(Intent.ACTION_SEND); 
      emailIntent.setData(Uri.parse("mailto:")); 
      emailIntent.setType("image/jpg"); 
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.mail_partage_objet)); 
      emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(readEmailTemplate())); 
      String imageFilePath=Constants.PHOTO_CACHE_PATH+"/"+currentPlace.getPhotoFileName(); 
      Log.d(TAG,"Picture Path: "+imageFilePath); 
      emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
      emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageFilePath)); 
      startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.email_share))); 

其中PHOTO_CACHE_PATH是圖像保存目錄的路徑,它是在SD卡

+0

Houcine嗨,我是沒有任何的Android開發者,但要考慮幾件事情,是連接和電子郵件不可見的圖像或根本不附帶?如果您嘗試保存附加的文件作爲新文件名,會發生什麼情況?您能查看這個新文件名嗎? – Neo 2012-01-27 14:27:54

+0

@Neo:你應該問問發佈這個問題的人,而不是我:),我剛剛編輯了這個問題來組織代碼^^ – Houcine 2012-01-27 14:38:10

+0

@Houcine大聲笑是的,我應該但是更容易輸入你的名字, user748677它使他們聽起來像一個統計數字;) – Neo 2012-01-27 14:47:02

回答

0

我想你忘記了:emailIntent.setType("application/octet-stream");

爲multiattachments

例子:

Intent exportMessageIntent = new Intent(
      android.content.Intent.ACTION_SEND_MULTIPLE); 
    exportMessageIntent.setType("text/plain"); 
    exportMessageIntent.setType("application/octet-stream"); 
    ArrayList<Uri> uris = new ArrayList<Uri>(); 

      //array of urls to your files on device - they are strings 
    filePaths = new String[] { "path1","path2" }; //for your case just insert imageFilePath -> filePaths = new String[] { imageFilePath }; 
      //create files from string array of paths 
    for (String file : filePaths) { 
     File fileIn = new File(file); 
     Uri u = Uri.fromFile(fileIn); 
     uris.add(u); 
    } 
    exportMessageIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, 
      uris); 

    exportMessageIntent 
      .putExtra(Intent.ACTION_DEFAULT, "test/"); 

    exportMessageIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject text"); 

    exportMessageIntent.putExtra(Intent.EXTRA_TEXT,"bodytext"); 

    startActivity(exportMessageIntent); 

希望它能幫助, 託尼

+0

託尼,他已將mimetype設置爲jpeg圖像而不是octet-stream – Neo 2012-01-27 16:29:03

+0

是的,我沒有看到。但是如果有人需要它,我會離開工作的例子。也許user748677關於你的第一個評論Neo。我應該看到附件,如果他通過郵件分享(在他發送郵件之前)。我同意Neo。 – toni 2012-01-30 06:28:08