Question

In my app, I'm creating a file. Sometimes my app crashes though, which is because the filename contains characters that are not allowed. I know I can remove those characters using String.replace(), but I don't know which characters are forbidden. So my question: which characters are forbidden or is there maybe a method I can use to remove all forbidden characters?

Était-ce utile?

La solution 2

I guess this one will help you:

It reads that all UTF-8 are allowed (by default) but that there are some reserved characters.

With which do you have problems?

Autres conseils

There are several approaches to sanitize a file name, you could settle for the lowest common practical, which would be a small subset of ASCII characters [A-Z|0-9|_]. Most file systems will handle a much larger set of possible characters in names, but its always a little platform specific.

Generally path separators, non-printable characters and characters with special meanings are explicitly forbidden in most common file systens or should be avoided because they pose trouble in many contexts.

Furthermore, while some file system do allow certain characters (square brackets and \ are infamous examples), they pose a lot of trouble if you need to exchange such a file with a system that does not allow them (anecdotal: Try feeding a Window95 System a CD using IS0-9660 filenames containing "\" in file names - havoc guaranteed).

So, while Android only forbids its own subset of characters in name, it generally not a bad idea to forbid any special character that is forbidden in any other common file system for the sake of interoperability.

A generic list of characters that shouldn't be used in file names: http://www.mtu.edu/umc/services/web/cms/characters-avoid/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top