簡體   English   中英

嘗試將文件從 SD 卡附加到 email

[英]Trying to attach a file from SD Card to email

我正在嘗試啟動發送 email 的意圖。 所有這些都有效,但是當我嘗試實際發送 email 時,會發生一些“奇怪”的事情。

這是代碼

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/filename.jpg"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the photo");
startActivity(Intent.createChooser(sendIntent, "Email:"));

因此,如果我使用 Gmail 菜單上下文啟動它會顯示附件,讓我輸入 email 是誰,然后編輯正文和主題。 沒什么大不了。 我點擊發送,它發送。 唯一的問題是附件沒有發送。

所以。 我想,為什么不嘗試使用 Email 菜單上下文(用於我手機上的備份 email 帳戶)。 它顯示附件,但正文或主題中根本沒有文本。 當我發送它時,附件會正確發送。 那會讓我相信有些事情是完全錯誤的。 我是否需要在 Manifest 啟動時獲得新許可才能發送帶有附件的 email? 我究竟做錯了什么?

也遇到同樣的問題

代碼:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("image/jpeg");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] 
    {"me@gmail.com"}); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
    "Test Subject"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
    "go on read the emails"); 
    Log.v(getClass().getSimpleName(), "sPhotoUri=" + Uri.parse("file:/"+ sPhotoFileName));
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ sPhotoFileName));
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

從亞行日志貓:

V/DumbDumpersMain( 3972):   sPhotoUri=file://sdcard/DumbDumpers/DumbDumper.jpg
I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.CHOOSER comp={android/com.android.internal.app.ChooserActivity} (has extras) }
I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x3000000 comp={com.google.android.gm/com.google.android.gm.ComposeActivityGmail} (has extras) }
I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x2800000 comp={com.google.android.gm/com.google.android.gm.ComposeActivity} (has extras) }
D/gmail-ls(  120):      MailProvider.query: content://gmail-ls/labels/me@gmail.com(null, null)
D/Gmail   ( 2507):      URI FOUND:file://sdcard/DumbDumpers/DumbDumper.jpg

看起來 email 提供程序正在附加一個長度為 0 的文件。 當我檢查文件系統時,文件在那里並且正確。 在嘗試 email 之前,創建圖像文件的代碼已經完成。

有人在沒有魔法重啟的情況下解決了這個問題(我已經嘗試過了)?

問候,

更新

我的道路應該是

file:///sdcard/DumbDumpers/DumbDumper.jpg

你需要額外的/因為它指向根目錄,即:

file:// + /sdcard/DumbDumpers/DumbDumper.jpg

結合為

file:///sdcard/DumbDumpers/DumbDumper.jpg

在上面的代碼片段中,您需要:

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

我希望這有幫助。 我花了很長時間來調試。

問候,
芬萊

只是我這邊的一點評論。 我在使用 GMail 時遇到了同樣的問題,但不知何故,當我首先將有問題的文件存儲在 SD 卡上並從那里而不是從資產中檢索它時,它似乎可以工作。 所以我的代碼如下:

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Send mail"));

和這里,

uri = Uri.fromFile(new File(context.getFilesDir(), FILENAME));

不起作用,而

uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));

做。

問候,邁克爾

而不是 "Uri.parse" 使用 "Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"file name"))"

Environment.getExternalStorageDirectory() - SD 卡或任何其他外部存儲的路徑

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"example@mail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "Data from app");
    i.putExtra(Intent.EXTRA_TEXT   , "experience number x");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "filename.txt"));
    i.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(i, "Send email..."));

看來這實際上是正確的,不確定發生了什么,但重新啟動后它開始工作:/

我遇到了同樣的問題,到處尋找解決方案。 最后,我通過找到一個開箱即用的開源應用程序解決了這個問題,並研究了他們是如何做到的。 代碼比較長,所以我不會在這里引用它,而是發布一個鏈接。 查看第 449 行中的 sendEmail function

http://rehearsalassist.svn.sourceforge.net/viewvc/rehearsalassist/android/trunk/src/urbanstew/RehearsalAssistant/SessionPlayback.java?revision=94&view=markup

我將我的代碼重構為相似,現在它可以工作了。 我希望這將幫助其他處於相同情況的人。

RFC 1738第 3.10 節:

文件 URL 采用以下形式:

   file://<host>/<path>

其中host是可以訪問路徑的系統的完全限定域名, path目錄/目錄/.../name形式的分層目錄路徑。

所以它是 file:///path/from/root 就像http://host/path/from/root因為在第二個和第三個斜杠之間有一個隱含的“localhost”。 但是如上所述,使用 Uri.FromFile() 來構建它。

我有同樣的症狀。 就我而言,這是因為我最初使用MODE_PRIVATE權限保存附件。 一旦我將其更改為MODE_WORLD_READABLE ,似乎 GMail 就能夠訪問該文件並正確發送附件。

看更多

這對我來說非常完美:在這個解決方案中,Nicolas 在 Cache 文件夾中創建了一個副本,並且 gmail 意圖可以訪問! http://stephendnicholas.com/archives/974

public void sendMail(String path) {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
        new String[] {AppConstant.server_mail});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
        "IBPS ERROR Mail");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
        "This is an autogenerated mail from IBPS app");
        emailIntent.setType("image/png");
        Uri myUri = Uri.parse("file://" + path);
        emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }

也嘗試添加 Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 這有助於解決我的問題。

4天后我得到了解決方案,請注意以下幾點,同時在Android(Java)中給出文件class的路徑:

1) 內部存儲使用路徑 String path="/storage/sdcard0/myfile.txt";

2) path="/storage/sdcard1/myfile.txt";

3)在清單文件中提及權限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

4) 首先檢查文件長度以確認。

5) 檢查 ES File Explorer 中關於 sdcard0 和 sdcard1 的路徑是否相同,否則......

例如

File file=new File(path); 
long=file.length();//in Bytes

發送帶有附件的 email:(通過文檔)

意圖 emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);

emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"});

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "電子郵件主題"); emailIntent.putExtra(Intent.EXTRA_TEXT, "電子郵件信息文本"); emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));

// 您也可以通過傳遞 Uris 的 ArrayList 來附加多個項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM