Save file in Android with spaces in file name
Asked Answered
G

3

7

I need for my android application to save an xml file into the external cache with a file name which contains space.

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(activity
                    .getExternalCacheDir().getAbsolutePath()
                    + "/Local storage.xml"));

transformer.transform(source, result);

When I browse manually into my file directory I find this file : "Local%20storage.xml".

So after when I try to read it with

File localStorageXmlFile = new File(activity.getExternalCacheDir()
                .getAbsolutePath()
                + "/Local storage.xml");

But I have a FileNotFoundException because the file "Local storage.xml" can't be found on my device.

Any ideas to solve this? Seb

Gradatim answered 24/4, 2012 at 16:4 Comment(2)
you should not build filenames yourself, use new File(activity.getExternalCacheDir(), "Local storage.xml") - it will do that for you. No idea what causes your problem thoughCircumnavigate
Thanks for this answer but this doesn't solve my problem.Gradatim
G
7

It was hard to identify the source of this problem but it comes from StreamResult which replaces spaces in file name by %20. There is a bug report for this issue here.

And here is the solution :

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(new File(activity
                .getExternalCacheDir().getAbsolutePath()
                + "/" + "Local storage" + ".xml"));
    Result fileResult = new StreamResult(fos);
    transformer.transform(source, fileResult);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        fos.close();
    }
}

Thanks again to both of you for trying to solve my issue.

Gradatim answered 25/4, 2012 at 8:34 Comment(1)
Thanks for the answer. You saved my day there. I fixed the problem the same way you did.Eucken
B
4

This works for me and is just one extra line of code

String fileName = "name with spaces" + ".xml";                  
fileName.replace(" ", "\\ ");
Butanone answered 12/5, 2013 at 15:46 Comment(2)
Forward slashes or back slashes?Attitude
Sorry about that back slashes I'll correct it. Hope it works for you.Butanone
S
1

Try using basic URL encoding :

File localStorageXmlFile = new File(activity.getExternalCacheDir()
                .getAbsolutePath()
                + "/Local%20storage.xml");

This mgiht help you : URL encoding table

Also, when working with file, make sure you're using the right file separator for the OS (in your case the hard coded / should work since Android is linux based but just to make sure. This could be an other option :

File localStorageXmlFile = new File(activity.getExternalCacheDir()
                .getAbsolutePath() + File.separator 
                + "Local storage.xml");

Last resort optin would be to try to espace the space. Try replacing " " with "\ ".

Speckle answered 24/4, 2012 at 16:10 Comment(2)
I try your solution Erwald before I post this question and I don't have the exception, but it didn't do what i'm expecting. Indeed, it creates an xml file named "Local%20storage.xml" and not "Local storage.xml".Gradatim
Your second answer doesn't work too, and you can't escape spaces like this "\ " in java.Gradatim

© 2022 - 2024 — McMap. All rights reserved.