无法在 android 中共享名称中包含空格的文件

Not able to share file with whitespace in name in android

如果文件名中有任何空格,我将无法在 whatsapp 中共享音频文件。但它在使用电子邮件客户端共享时有效。对于没有空格的文件名也可以正常工作。下面是我正在使用的代码

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File F = new File(filePath);
F.setReadable(true, false);
Uri fileURI = Uri.fromFile(F);
Log.e("Share", "Share file url is " + fileURI);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Shared file");
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileURI);

我试过 filePath.replace(" ", "\ "),但没有用。 应该做哪些更改才能共享文件?

这在您尝试与 WhatsApp 共享带有空格的音频文件时有效:

String filePath = "file:///sdcard/Download/example attachment.mp3";

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
shareIntent.setType("audio/*");
startActivity(Intent.createChooser(shareIntent, "Share audio file"));

我能够使用与我发布的代码相同的代码共享音频,只是类型稍作更改。以下代码适用于电子邮件和 whatsapp 共享:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File F = new File(filePath);
F.setReadable(true, false);
Uri fileURI = Uri.fromFile(F);
Log.e("Share", "Share file url is " + fileURI);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Shared file");
sharingIntent.setType("audio/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileURI);