Android 上的文件名允许使用哪些字符?

发布于 2024-08-29 10:51:47 字数 112 浏览 1 评论 0原文

Android 上的文件名允许使用哪些特殊字符?

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

另外,我可以用 Unicode 名称保存文件吗?

What special characters are allowed for file names on Android?

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

Also, can I save file with Unicode name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

套路撩心 2024-09-05 10:51:47
  1. 在 Android 上(至少默认情况下),文件名编码为 UTF-8。

  2. 看起来保留的文件名字符取决于安装的文件系统(http://en.wikipedia.org/wiki /Filename)。

我认为保留:

private static final String ReservedChars = "|\\?*<\":>+[]/'";
  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 = "|\\?*<\":>+[]/'";
请恋爱 2024-09-05 10:51:47

来自 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;
        }
    }

注意:FileUtils 是隐藏的 API(不适用于应用程序;适用于 AOSP 使用)。用作参考(或通过反思,风险自负)

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)

冷情妓 2024-09-05 10:51:47

根据 wiki 并假设您使用的是 FAT32 的外部数据存储。

目录条目中允许的字符

除值 0-31、127 (DEL) 和:“ * / : < > ? \ | + , . ; = [](小写 az 存储为 AZ)之外的任何字节。使用 VFAT LFN 任何 Unicode除了 NUL

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

原来分手还会想你 2024-09-05 10:51:47
final String[] ReservedChars = {"|", "\\", "?", "*", "<", "\"", ":", ">"};

for(String c :ReservedChars){
    System.out.println(dd.indexOf(c));
    dd.indexOf(c);
}
final String[] ReservedChars = {"|", "\\", "?", "*", "<", "\"", ":", ">"};

for(String c :ReservedChars){
    System.out.println(dd.indexOf(c));
    dd.indexOf(c);
}
迷爱 2024-09-05 10:51:47

这是 Android 中文件名的正确 InputFilter

    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;
        }  
    };

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;
        }  
    };
你的背包 2024-09-05 10:51:47

这显然取决于文件系统和 Android 操作系统。 唯一字符

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

在我的 oneplus/oxygenOS 上,接受的答案中我无法用来重命名文件的

是 / 和 *但是,在 Android 范围内,上面的列表似乎是合理的。

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.

夏天碎花小短裙 2024-09-05 10:51:47

我在 Android 上的 Galaxy Note 8 上快速进行了测试4.4.2.默认的“我的文件”应用程序会有效地将无效字符显示为灰色,如下所示:

? : " * | / \ <>

我将所有其他可用的特殊字符放入文件名中并保存。这在所有 Android 版本中可能不一致,因此最好保守一点,并将它们替换为类似有意义的字符。

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.

别在捏我脸啦 2024-09-05 10:51:47

在 Android 上按照建议,您可以使用输入过滤器来防止用户输入无效字符,这里是一个更好的实现:

/**
 * 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
        }
    }
}

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
        }
    }
}
风尘浪孓 2024-09-05 10:51:47

在 Android 中,标准键盘上出现的每个符号都是允许的

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文