簡體   English   中英

如何使用 Sketchware 修改 Java 代碼以發送多個 email 附件?

[英]How to amend Java code to send multiple email attachments, using Sketchware?

我對 Java 真的很陌生,並且一直在 Sketchware 上構建應用程序。 如果您不熟悉它,它使用塊編程,您可以在自定義塊中注入自己的代碼。

由於所有應用程序視圖的存儲都是本地的,因此我需要通過按下按鈕將所有 output PDF 附加到 email。

下面的代碼可以附加一個文件,但需要附加 6 個文件。 所有這些都從 android 設備上的 /Documents/ 文件夾中調用。 我怎樣才能做到這一點?

emailIntent.putExtra(
    Intent.EXTRA_STREAM,
    Uri.fromFile(
        new java.io.File(Environment.getExternalStorageDirectory() +"/Documents/filename.pdf")
    )
);

我擁有的文件名在一個文件夾中,名為filename1.pdffilename2.pdf等。

如果我嘗試對每個文件名重復此代碼,則filename6.pdf將是附加到 email 的唯一文件。

這是 Sketchware 的框圖:

在此處輸入圖像描述

首先,正如其他答案所暗示的,目前Intent.ACTION_SEND_MULTIPLE是發送多個文件的方式。

但是,在 Sketchware 的內置塊中沒有功能並不是應用程序的真正限制,因為它提供了以下塊,它能夠以 android 的方式做任何你想做的事情。

在此處輸入圖像描述

您已經使用此元素添加了一些自定義代碼。 因此,為了解決您的問題,該塊將如下所示:

在此處輸入圖像描述

以下是我添加的一些自定義代碼塊的詳細信息:

mail.setAction(Intent.ACTION_SEND_MULTIPLE):已通過刪除默認Intent > setAction塊添加此自定義代碼。 動作名稱說明了一切,這允許通過意圖發送多個文件。

ArrayList<Uri> uris = new ArrayList<Uri>():這聲明了一個新的ArrayList來存儲要通過意圖發送的所有 Uri 的列表。

uris.add(Uri.fromFile(new java.io.File(Environment.getExternalStorageDirectory() + "/Documents/filename1.pdf"))): This line adds the provided uri to the ArrayList named uris . 只要您想將多個文件 uri 添加到列表中,就可以多次調用此塊。

mail.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris):這會將uris綁定到意圖的EXTRA_STREAM

編輯:

從 Android 7.0 及更高版本開始,出於安全目的,有一些策略更改。 這就是添加此額外代碼的原因。 上面的塊圖像已經使用此代碼更新:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

雖然建議使用android.support.v4.content.FileProvider來解決此類問題,但對於 Sketchware 平台的支持較少,此時最好使用上述方法。

您可以閱讀內容以進一步了解上述修復。

也許這將為您完成工作。

這是創建包含多個附件的emailIntent所需的代碼。 關鍵變化是ACTION_SEND_MULTIPLE

public static void email(Context context, String emailTo, String emailCC,
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
        new String[]{emailCC});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
    //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(Intent.createChooser(emailIntent, "Send mail..."));
}

更新

在聊天室中發表討論 我想得出結論,無法使用 Sketchware 在 email 中發送多個附件,因為它不提供Intent.ACTION_SEND_MULTIPLE功能。 您需要一一發送多封帶有附件的電子郵件。

當您有編碼的自由時,上面提到的代碼就足夠了您需要的工作,這將與這里提到的 Android 一起使用。

我能讀到的關於 Sketchware 的所有信息都是一次只能附加一個文件, 請參見此處

暫無
暫無

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

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