What characters allowed in file names on Android?
Asked Answered
D

9

58

What special characters are allowed for file names on Android?

~!@#$%^&*()_+/\.,

Also, can I save file with Unicode name?

Dunning answered 21/4, 2010 at 1:10 Comment(3)
Do you mean on the SD card (or equivalent) ?Mar
superuser.com/a/693819/687273Aggrade
On Unix (and thus Android), it’s simply NULL (0x00) and / which are invalid. For interoperability, though, you may be interested in the Windows list from the answers below.Shafting
D
41
  1. On Android (at least by default) the file names encoded as UTF-8.

  2. Looks like reserved file name characters depend on filesystem mounted (http://en.wikipedia.org/wiki/Filename).

I considered as reserved:

private static final String ReservedChars = "|\\?*<\":>+[]/'";
Dunning answered 24/4, 2010 at 9:16 Comment(3)
'+[] are not reservedKirbykirch
Without '+[] (as @Kirbykirch noted), this is actually the Windows set. But it describes the invalid printable characters only. The control characters 0x00-0x1f and 0x7f are also invalid on Windows. For interoperability, all this may be useful. But on Unix (and thus Android) alone, the only invalid characters are NULL (0x00) and /.Shafting
Came here researching firefox downloads on android. For some reason firefox thinks the plus sign makes for an invalid filename.Lachus
B
21

From android.os.FileUtils

    private static boolean isValidFatFilenameChar(char c) {
        if ((0x00 <= c && c <= 0x1f)) {
            return false;
        }
        switch (c) {
            case '"':
            case '*':
            case '/':
            case ':':
            case '<':
            case '>':
            case '?':
            case '\\':
            case '|':
            case 0x7F:
                return false;
            default:
                return true;
        }
    }
    private static boolean isValidExtFilenameChar(char c) {
        switch (c) {
            case '\0':
            case '/':
                return false;
            default:
                return true;
        }
    }

Note: FileUtils are hidden APIs (not for apps; for AOSP usage). Use as a reference (or by reflection at one's own risk)

Broaddus answered 23/9, 2020 at 5:12 Comment(0)
P
10

According to wiki and assuming that you are using external data storage which has FAT32.

Allowable characters in directory entries

are

Any byte except for values 0-31, 127 (DEL) and: " * / : < > ? \ | + , . ; = [] (lowcase a-z are stored as A-Z). With VFAT LFN any Unicode except NUL

Pancreatin answered 21/11, 2012 at 21:2 Comment(2)
"; , . = " are allowed for file names in androidSparkle
Is it true on both external and internal storage? Are all of the rest of the characters allowed? Do lowercard=uppercase as the text is showing here (meaning I can't have "Hello.txt" and "hello.txt" on the same folder, for example) ?Waldemar
P
7
final String[] ReservedChars = {"|", "\\", "?", "*", "<", "\"", ":", ">"};

for(String c :ReservedChars){
    System.out.println(dd.indexOf(c));
    dd.indexOf(c);
}
Providence answered 7/12, 2011 at 13:29 Comment(0)
P
6

This is correct InputFilter for File Names in Android:

    InputFilter filter = new InputFilter()
    {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) 
        { 
            if (source.length() < 1) return null;
            char last = source.charAt(source.length() - 1);
            String reservedChars = "?:\"*|/\\<>";
            if(reservedChars.indexOf(last) > -1) return source.subSequence(0, source.length() - 1);
            return null;
        }  
    };
Priggery answered 14/2, 2015 at 14:17 Comment(1)
Good idea, poor implementation. As it is it will only filter properly when entering characters one by one. If you paste something in chances are it won't be filtered properly.Disaccord
F
3

This is clearly filesystem and Android operating system dependent. On my oneplus/oxygenOS, the only characters in the accepted answer

private static final String ReservedChars = "|\\?*<\":>+[]/'";

that I could not use to rename a file were / and *

However, Android wide, the list above would seem to be sensible.

Familiar answered 10/1, 2018 at 8:9 Comment(0)
G
2

I tested this quickly on my Galaxy Note 8 on Android 4.4.2. The default My Files app helpfully greys out invalid characters which are as follows:

? : " * | / \ < >

I put all the other special chars available into a filename and it saved. This may not be consistent across all Android versions so maybe it's best to be conservative and replace them with similarly meaningful characters.

Gerhard answered 4/11, 2014 at 23:36 Comment(2)
What is a Galaxy Note 8? Especially in 2014.Simony
From Samsung's website: Note TabletGerhard
D
2

On Android as suggested there you can use an input filter to prevent user entering invalid characters, here is a better implementation of it:

/**
 * An input filter which can be attached to an EditText widget to filter out invalid filename characters
 */
class FileNameInputFilter: InputFilter
{
override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence? {
    if (source.isNullOrBlank()) {
        return null
    }

    val reservedChars = "?:\"*|/\\<>\u0000"
    // Extract actual source
    val actualSource = source.subSequence(start, end)
    // Filter out unsupported characters
    val filtered = actualSource.filter { c -> reservedChars.indexOf(c) == -1 }
    // Check if something was filtered out
    return if (actualSource.length != filtered.length) {
        // Something was caught by our filter, provide visual feedback
            if (actualSource.length - filtered.length == 1) {
                // A single character was removed
                BrowserApp.instance.applicationContext.toast(R.string.invalid_character_removed)
            } else {
                // Multiple characters were removed                    
     BrowserApp.instance.applicationContext.toast(R.string.invalid_characters_removed)
                }
            // Provide filtered results then
            filtered
        } else {
            // Nothing was caught in our filter
            null
        }
    }
}
Disaccord answered 10/1, 2021 at 17:31 Comment(0)
F
0

In Android every single symbol that appears on a standard keyboard is allowed 𝘦𝘹𝘤𝘦𝘱𝘵 𝘧𝘰𝘳 𝘰𝘯𝘦: the forward slash (" / ").

That means that the following five symbols, in particular, all of which are 𝘥𝘪𝘴allowed in Windows...

⁠      *  ?  "  :  \

..are allowed to be used in Android filenames. Only the forward slash will generate an error.

That said, it's not impossible that certain Android apps may balk at opening/saving files whose names contain some of the allowed symbols.

Fancywork answered 18/3 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.