What characters allowed in file names on Android?

2019-01-02 15:42发布

What special characters are allowed for file names on Android?

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

Also, can I save file with Unicode name?

6条回答
美炸的是我
2楼-- · 2019-01-02 16:24

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;
        }  
    };
查看更多
高级女魔头
3楼-- · 2019-01-02 16:28

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.

查看更多
余生无你
4楼-- · 2019-01-02 16:28

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.

查看更多
流年柔荑漫光年
5楼-- · 2019-01-02 16:31

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

查看更多
后来的你喜欢了谁
6楼-- · 2019-01-02 16:32
  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 = "|\\?*<\":>+[]/'";
查看更多
大哥的爱人
7楼-- · 2019-01-02 16:39
final String[] ReservedChars = {"|", "\\", "?", "*", "<", "\"", ":", ">"};

for(String c :ReservedChars){
    System.out.println(dd.indexOf(c));
    dd.indexOf(c);
}
查看更多
登录 后发表回答