rydgaze
rydgaze

Reputation: 1060

Opening a local Android File with spaces

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

Upvotes: 5

Views: 4163

Answers (2)

rydgaze
rydgaze

Reputation: 1060

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.

Upvotes: 1

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12672

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

Upvotes: 1

Related Questions