如何以编程方式在同一 Android 意图中共享不同 MIME 类型的多个文件?
How to programmatically share multiple files of different MIME types within the same Android intent?
我正在开发一个 Android 应用程序,该应用程序已经使用以下方法通过蓝牙成功共享生成的 PDF 文件:
public static void sharePdfFile(Context ctx, String pathAndFile) {
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.android.bluetooth");
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile));
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
有人要求我在此共享意向中包含第二个文件(CSV 格式),以便将两个文件一起发送。我立即找到 this question,它解决了通过蓝牙发送多个文件的问题,但仅使用相同 MIME 类型的文件(在该示例中为 "video/*"。)
我发现了很多通配符 MIME 子类型("video/*"、"text/*" 等)的示例,但此时我无法找到具有多个特定 MIME 类型集的 Intent 的任何示例(示例:"application/pdf" 和 "text/comma-separated-values")。因此,我创建了一个使用“*/*”作为 MIME 类型的测试方法,希望能达到目的。不幸的是,我的测试方法甚至还不足以激活蓝牙共享弹出窗口到 select 附近的设备。
我不确定我做错了什么或遗漏了什么。我似乎无法在调试时捕获任何错误,所以我认为我仍然遗漏了一些东西。我知道 PDF 和 CSV 文件及其各自的 URI 都可以,因为这两个文件都可以通过原始方法正常传输(我更改了现有方法的 MIME 类型和 URI 以测试新的 CSV 文件。)
这是我的测试方法:
public static void shareTwoFilesTest(Context ctx, String pathAndFile, String pathAndFile2) {
try {
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setPackage("com.android.bluetooth");
share.setType("*/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile2));
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
在完成最终草稿后,我最终找到了 question/issue 的可行解决方案。我在写作时一直在研究这个问题,发现 this answer 表明我的测试方法中缺少 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
。我添加了,但我的测试方法仍然无效。
然后我发现 this answer 表明也许我应该将 URI 的数组列表添加到我的意图中,而不是尝试添加多个单个 URI。添加最后一个缺失的部分后,我得到了一个可以正常运行的测试函数:
public static void shareTwoFilesTest(Context ctx, String pathAndFile, String pathAndFile2) {
ArrayList<Uri> Uris = new ArrayList<>();
Uris.add(Uri.parse(pathAndFile));
Uris.add(Uri.parse(pathAndFile2));
try {
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setPackage("com.android.bluetooth");
share.setType("*/*");
share.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
share.putExtra(Intent.EXTRA_STREAM, Uris);
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
如果您认为这个答案可以改进或有其他解决问题的方法,请随时回答或发表您认为合适的评论。我继续发布了问题和我的答案,希望这可以在将来帮助其他人。
我正在开发一个 Android 应用程序,该应用程序已经使用以下方法通过蓝牙成功共享生成的 PDF 文件:
public static void sharePdfFile(Context ctx, String pathAndFile) {
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.android.bluetooth");
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile));
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
有人要求我在此共享意向中包含第二个文件(CSV 格式),以便将两个文件一起发送。我立即找到 this question,它解决了通过蓝牙发送多个文件的问题,但仅使用相同 MIME 类型的文件(在该示例中为 "video/*"。)
我发现了很多通配符 MIME 子类型("video/*"、"text/*" 等)的示例,但此时我无法找到具有多个特定 MIME 类型集的 Intent 的任何示例(示例:"application/pdf" 和 "text/comma-separated-values")。因此,我创建了一个使用“*/*”作为 MIME 类型的测试方法,希望能达到目的。不幸的是,我的测试方法甚至还不足以激活蓝牙共享弹出窗口到 select 附近的设备。
我不确定我做错了什么或遗漏了什么。我似乎无法在调试时捕获任何错误,所以我认为我仍然遗漏了一些东西。我知道 PDF 和 CSV 文件及其各自的 URI 都可以,因为这两个文件都可以通过原始方法正常传输(我更改了现有方法的 MIME 类型和 URI 以测试新的 CSV 文件。)
这是我的测试方法:
public static void shareTwoFilesTest(Context ctx, String pathAndFile, String pathAndFile2) {
try {
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setPackage("com.android.bluetooth");
share.setType("*/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile2));
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
在完成最终草稿后,我最终找到了 question/issue 的可行解决方案。我在写作时一直在研究这个问题,发现 this answer 表明我的测试方法中缺少 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
。我添加了,但我的测试方法仍然无效。
然后我发现 this answer 表明也许我应该将 URI 的数组列表添加到我的意图中,而不是尝试添加多个单个 URI。添加最后一个缺失的部分后,我得到了一个可以正常运行的测试函数:
public static void shareTwoFilesTest(Context ctx, String pathAndFile, String pathAndFile2) {
ArrayList<Uri> Uris = new ArrayList<>();
Uris.add(Uri.parse(pathAndFile));
Uris.add(Uri.parse(pathAndFile2));
try {
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setPackage("com.android.bluetooth");
share.setType("*/*");
share.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
share.putExtra(Intent.EXTRA_STREAM, Uris);
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
如果您认为这个答案可以改进或有其他解决问题的方法,请随时回答或发表您认为合适的评论。我继续发布了问题和我的答案,希望这可以在将来帮助其他人。