打开带有空格的本地Android文件 [英] Opening a local Android File with spaces

查看:587
本文介绍了打开带有空格的本地Android文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开Android的使用意图本地文件(4.0)。以下是code做的动作。这只要正常工作的文件没有特殊的空间(例如:如果文件是/data/data/com.xxxx.yyyy/files/Downloads/Documents/ProductFeature.pptx时,它会打开罚款,但如果该文件名称是/data/data/com.xxxx.yyyy/files/Downloads/Documents/Product Feature.pptx(注意在名称空间),则它将失败。该Uri.fromFile烯codeS空间正确,但其他应用程序不能似乎间preT他们,似乎无法打开。

I am trying to open a local file in Android (4.0) using intents. The following is the code to do the action. This works fine as long as the file has no special spaces (For example: if the file is /data/data/com.xxxx.yyyy/files/Downloads/Documents/ProductFeature.pptx, the it opens fine, but if the file name is /data/data/com.xxxx.yyyy/files/Downloads/Documents/Product Feature.pptx (note the space in name), then it fails. The Uri.fromFile encodes the space correctly, but the other apps cant seem to interpret them and seem fail opening.

        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 ...")); 

我也试过用文件://+ unen codeD路径没有太大的帮助。

I also tried to use "file://" + unencoded path without much help.

那么,你如何处理这种情况?任何帮助AP preciated

So how do you handle this condition? Any help is appreciated

推荐答案

您需要使用来代替空格\\。请注意,如果您使用的是与string.replace(),那么你需要逃跑的斜线以及(\\\\)。

You need to replace the spaces with "\ ". Note that if you are using String.replace() then you need to escape the slash as well ("\\ ").

这篇关于打开带有空格的本地Android文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆