2011-11-30 122 views
3

我無法發送電子郵件的附件。在Android中發送PDF作爲電子郵件附件

首先:我正在使用iText5將一些文本標記爲PDF。 PDF存儲在我的資產文件夾中:

AssetManager am = getAssets(); 
InputStream fs; 
FileOutputStream fOut ; 

try 
{ 
    fs = am.open("RFA2_10_11.pdf"); 
    PdfReader pdfReader = new PdfReader(fs); 
    Log.d("pdfreader", pdfReader.getFileLength()+" kello"); 
    fOut = openFileOutput("DocStamped.pdf", 
    MODE_WORLD_READABLE); 

    PdfStamper stamper = new PdfStamper(pdfReader, fOut); 
    PdfContentByte canvas = stamper.getOverContent(1); 

    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Stefano Molle"), 250, 710, 0); 
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("4 Saint Pappins Road, Glasnevin, Dublin 11"), 250, 645, 0); 

    stamper.close(); 
} 
catch (IOException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
catch (DocumentException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

因此,創建的PDF存儲在我的應用程序的根文件目錄中。

我需要保存的文件DocStamped.pdf附件的形式發送在一封電子郵件:

String emailTo = "[email protected]"; 
//lets send the email 

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 
emailIntent.setType("text/plain"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailTo});    
ArrayList<Uri> uris = new ArrayList<Uri>(); 

File file = new File(getFilesDir()+"/"+"DocStamped.pdf"); 
Log.d("file", file.getAbsolutePath()); 

Uri uri = Uri.fromFile(file); 
uris.add(uri); 
emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 

// emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

現在,這是我的代碼來發送電子郵件。但我有麻煩。在應用程序上說有附件,但是當我在瀏覽器中檢查我的電子郵件帳戶時,沒有附件只是我寫入的文本。

任何想法傢伙爲什麼附件未發送?

回答

-1

如果您使用的是Gmail客戶端,則附件路徑必須以mnt/sdcard開頭。

+0

但我不認爲應用程序將文件存儲在SD卡上, – molleman

+0

好吧,但這是gmail客戶端的限制。如果您想要支持Gmail客戶端,則必須從SD卡連接文件 – mihail

0

你的代碼工作對我來說,有一對夫婦的修改:

  • 我用getFilesDir()(/數據/數據/ [應用程序名] /文件)以檢索的PDF格式,而不是資產
      ArrayList<Uri>
    • insted的我用一個開放的,就像這樣:

Uri URI = Uri.parse("file://" + file.getAbsolutePath()); emailIntent.putExtra(Intent.EXTRA_STREAM, URI);

相關問題