Opening local android file with spaces

I am trying to open a local file in Android (4.0) using intents. Below is the code to perform 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 it opens fine, but if the file name is / data / data /com.xxxx.yyyy/files/Downloads/Documents/Product Feature.pptx (note the space in the name) then it doesn't work.Uri.fromFile encodes the space correctly, but other apps can't seem to interpret them and seem to fail.

        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 using "file: //" + unencoded path without much help.

So how do you deal with this condition? Any help is appreciated

+3


source to share


2 answers


You need to replace spaces with "\ "

. Note that if you are using String.replace()

, you also need to escape the forward slash ( "\\ "

).



+1


source


The main problem for me turned out to be that I was storing files in the internal storage application directory, and access to the files had to be specified explicitly. I granted permissions using chmod 755 command. But files with spaces did not get the correct permissions which prevented those files from opening.



I have since switched to using external (Activity.getExternalFilesDir ()) and this folder allows access to any other application and solved the problem for me.

0


source







All Articles