android - 打开带有空格的本地 Android 文件

标签 android

我正在尝试使用 intents 在 Android (4.0) 中打开一个本地文件。以下是执行该操作的代码。只要文件没有特殊空格,这就可以正常工作(例如:如果文件是/data/data/com.xxxx.yyyy/files/Downloads/Documents/ProductFeature.pptx,它可以正常打开,但如果文件名称是/data/data/com.xxxx.yyyy/files/Downloads/Documents/Product Feature.pptx(注意名称中的空格),然后它失败了。Uri.fromFile 正确编码了空间,但其他应用程序似乎不能解释它们似乎无法打开。

        Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);

    Uri uri =  Uri.fromFile(new File( selectedEntry.get(Defs.PATH_KEY)));
    System.out.println("openFileWith: File to open: " + uri);

    intent.setDataAndType(uri,type);
    startActivity(Intent.createChooser(intent, "Open With ...")); 

我也尝试过使用“file://”+未编码的路径,但没有太多帮助。

那么你如何处理这种情况?感谢任何帮助

最佳答案

您需要将空格替换为"\"。请注意,如果您使用的是 String.replace(),那么您还需要转义斜杠 ("\\")。

关于android - 打开带有空格的本地 Android 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488794/

相关文章:

android - android fragment 类中的当前纬度和经度

android - 如何在 Android webRTC 上启用 H264

android - 我可以如何处理我的应用程序中的自定义表情符号?

android - 使用 Jetpack 的 Android 导航组件销毁/重新创建的 fragment

Android - 使用指纹认证加密和解密数据

java - IInAppBillingService 陷入购买叠加层

android - 使用 adb screenrecord 命令通过 USB 将 Android 屏幕镜像到 PC

android - Eclipse 没有检测到我的 Android 设备

android - 如何在 React Native 中实现类似 snapchat 的圆形动画倒计时组件?

android - findViewById 仍然返回 null(新手)