4

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

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

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

rydgaze
  • 1,050
  • 13
  • 24
  • try "\ " instead of " " . I am not sure though, just give it a try – pvn Jan 23 '13 at 20:43
  • Try replacing the space with either a + sign or %20 or try adding double quotes around the name. – SylvainL Jan 23 '13 at 21:16
  • @pvn I tried that and it doesn't seem to help. Thanks for the suggestion. I didnt even try that before. – rydgaze Jan 23 '13 at 21:24
  • @SylvainL The %20 is generated when Uri.fromFile (as it encoded) and that doesnt work, I will try the "+" sign and report Thanks! – rydgaze Jan 23 '13 at 21:25
  • If you try with the double quotes, try with both the following versions: "/data/data/com.xxxx.yyyy/files/Downloads/Documents/ProductFeature.pptx" or /data/data/com.xxxx.yyyy/files/Downloads/Documents/"ProductFeature.pptx" – SylvainL Jan 23 '13 at 21:32
  • @SylvainL Tried both options of double quotes and same problem (I tried it with Uri.parse("file://" + name), Uri.fromFile(new File(name)) . – rydgaze Jan 23 '13 at 21:44
  • What kind of file/application is that? – SylvainL Jan 23 '13 at 21:46
  • @SylvainL This app downloads a file from server and then allows that file to opened with other installed apps. In this case I seems to launch polarisviewer app to open that pptx. – rydgaze Jan 23 '13 at 21:53

2 Answers2

1

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

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
1

The main problem for me turned out to be I was storing files in application directory of internal storage and the access to the files had to be given explicitly. I was giving permissions using chmod 755 command. But the files with spaces were not getting the permissions set correctly which prevented those files being opened.

I have since moved to use external (Activity.getExternalFilesDir()) and that folder allows access permission to every other application and that solved the issue for me.

rydgaze
  • 1,050
  • 13
  • 24