Ben*_*rth 0 android bluetooth android-intent mime-types
我正在开发一个 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我被要求在此共享意图中包含第二个文件(CSV 格式),以便两个文件一起发送。我立即发现了这个问题,它解决了通过蓝牙发送多个文件的问题,但仅使用相同 MIME 类型的文件(该示例中的“video/*”)。
我已经找到了大量通配符 MIME 子类型的示例(“video/*”、“text/*”等),但此时我无法找到具有多个特定 MIME 类型集的 Intent 的任何示例(例如:“应用程序/pdf”和“文本/逗号分隔值”)。因此,我创建了一个使用“*/*”作为 MIME 类型的测试方法,希望能够解决问题。不幸的是,我的测试方法甚至还不足以激活蓝牙共享弹出窗口以选择附近的设备。
我不确定我做错了什么或遗漏了什么。我在调试时似乎无法捕获任何错误,所以我认为我仍然遗漏了一些东西。我确实知道 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);
}
}
Run Code Online (Sandbox Code Playgroud)
在我完成最终草案后,我最终找到了解决我的问题/问题的可行解决方案。我在写作时不断研究这个问题,并发现这个答案表明我intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);在测试方法中遗漏了。我添加了这一点,但我的测试方法仍然不起作用。
然后我发现这个答案表明也许我应该将 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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您认为这个答案可以改进或有其他方法解决问题,请随意回答或发表您认为合适的评论。我继续发布问题和我的答案,希望这可以帮助将来的其他人。
| 归档时间: |
|
| 查看次数: |
3214 次 |
| 最近记录: |