如何使用 Sketchware 修改 Java 代码以发送多个电子邮件附件?
How to amend Java code to send multiple email attachments, using Sketchware?
我是 Java 的新手,一直在 Sketchware 上构建应用程序。如果您不熟悉它,它会使用块编程,您可以在自定义块中注入自己的代码。
由于所有应用视图的存储仅在本地,我需要按一个按钮将所有输出 PDF 附加到电子邮件中。
下面的代码可以附加一个文件,但需要附加 6 个文件。所有这些都是从 android 设备上的 /Documents/ 文件夹中调用的。我怎样才能做到这一点?
emailIntent.putExtra(
Intent.EXTRA_STREAM,
Uri.fromFile(
new java.io.File(Environment.getExternalStorageDirectory() +"/Documents/filename.pdf")
)
);
我的文件名都在一个文件夹里,分别命名为filename1.pdf
、filename2.pdf
等
如果我尝试对每个文件名重复此代码,filename6.pdf
将是附加到电子邮件的唯一文件。
这是 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..."));
}
更新
Post 聊天室讨论 我想得出的结论是,使用 Sketchware 无法在电子邮件中发送多个附件,因为它不提供 Intent.ACTION_SEND_MULTIPLE
功能。您需要一封一封发送多封带附件的邮件。
当您可以自由编写代码时,上述代码足以满足您的工作需求,并且可以与 Android as mentioned here.
一起使用
我对 Sketchware 的了解是一次只能附加一个文件,see here。
首先,正如其他答案所暗示的,目前Intent.ACTION_SEND_MULTIPLE
是发送多个文件的方式。
但是,在 Sketchware 的内置块中没有功能并不是应用程序的确切限制,因为它提供了以下块,它能够以 android 的方式做任何你想做的事情。
并且您已经使用此元素添加了一些自定义代码。所以,为了解决你的问题,块将是这样的:
下面是我添加的一些自定义代码块的详细信息:
mail.setAction(Intent.ACTION_SEND_MULTIPLE):
此自定义代码是通过删除默认 Intent > setAction 块添加的。动作名称说明了一切,这允许通过意图发送多个文件。
ArrayList<Uri> uris = new ArrayList<Uri>():
这声明了一个新的 ArrayList 来存储要通过 intent 发送的所有 Uri 的列表。
uris.add(Uri.fromFile(new java.io.File(Environment.getExternalStorageDirectory() + "/Documents/filename1.pdf"))):
此行将提供的 uri 添加到名为 uris[=60= 的 ArrayList ].多次调用此块,将多个文件 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平台支持较少,此时最好使用上述方法。
您可以阅读 this 以获得对上述修复的更多说明。
我是 Java 的新手,一直在 Sketchware 上构建应用程序。如果您不熟悉它,它会使用块编程,您可以在自定义块中注入自己的代码。
由于所有应用视图的存储仅在本地,我需要按一个按钮将所有输出 PDF 附加到电子邮件中。
下面的代码可以附加一个文件,但需要附加 6 个文件。所有这些都是从 android 设备上的 /Documents/ 文件夹中调用的。我怎样才能做到这一点?
emailIntent.putExtra(
Intent.EXTRA_STREAM,
Uri.fromFile(
new java.io.File(Environment.getExternalStorageDirectory() +"/Documents/filename.pdf")
)
);
我的文件名都在一个文件夹里,分别命名为filename1.pdf
、filename2.pdf
等
如果我尝试对每个文件名重复此代码,filename6.pdf
将是附加到电子邮件的唯一文件。
这是 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..."));
}
更新
Post 聊天室讨论 我想得出的结论是,使用 Sketchware 无法在电子邮件中发送多个附件,因为它不提供 Intent.ACTION_SEND_MULTIPLE
功能。您需要一封一封发送多封带附件的邮件。
当您可以自由编写代码时,上述代码足以满足您的工作需求,并且可以与 Android as mentioned here.
一起使用我对 Sketchware 的了解是一次只能附加一个文件,see here。
首先,正如其他答案所暗示的,目前Intent.ACTION_SEND_MULTIPLE
是发送多个文件的方式。
但是,在 Sketchware 的内置块中没有功能并不是应用程序的确切限制,因为它提供了以下块,它能够以 android 的方式做任何你想做的事情。
并且您已经使用此元素添加了一些自定义代码。所以,为了解决你的问题,块将是这样的:
下面是我添加的一些自定义代码块的详细信息:
mail.setAction(Intent.ACTION_SEND_MULTIPLE):
此自定义代码是通过删除默认 Intent > setAction 块添加的。动作名称说明了一切,这允许通过意图发送多个文件。
ArrayList<Uri> uris = new ArrayList<Uri>():
这声明了一个新的 ArrayList 来存储要通过 intent 发送的所有 Uri 的列表。
uris.add(Uri.fromFile(new java.io.File(Environment.getExternalStorageDirectory() + "/Documents/filename1.pdf"))):
此行将提供的 uri 添加到名为 uris[=60= 的 ArrayList ].多次调用此块,将多个文件 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平台支持较少,此时最好使用上述方法。
您可以阅读 this 以获得对上述修复的更多说明。