android:单个文件的pathPattern

Bar*_*ica 1 android intentfilter android-manifest mime-types

我需要定义IntentFilter名为 的单个文件myfile.ext。目前我的清单如下所示:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.EDIT" />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:scheme="file"
                android:mimeType="*/*"
                android:host="*"
                android:pathPattern=".*\\myfile\\.ext"
            />
        </intent-filter>
Run Code Online (Sandbox Code Playgroud)

我还尝试了其他变体,例如:android:pathPattern=".*\\myfile.ext"等等 - 但它仍然无法处理我的文件。

有什么线索吗?

Man*_*ani 6

Android 模式细节:

  • .匹配任何字符。
  • *匹配 0 次或多次出现的前面的字符。
  • .*匹配任意字符 0 次或多次出现。
  • \用于转义模式中的特殊字符。\当从 XML 文件读取字符串时,也用作转义字符。因此,为了转义模式中的特殊字符,\\必须使用双斜杠。

问题:在此模式中".*\\myfile\\.ext",您试图转义m普通字符。因此,这没有任何区别。它相当于".*myfile\\.ext". 意图的数据 uri 部分是file:///mnt/sdcard/tmp/myfile.ext。该模式与 匹配/mnt/sdcard/tmp/myfile.ext,但失败。

.*尝试匹配任何字符,直到第一次出现m,这恰好是第二个字符 ie /mnt模式期望下一个字符是y,但它得到了n,因此模式匹配失败。

解决方案:对于上述路径,该模式/.*/.*/.*/myfile\\.ext有效。

对于/mnt/sdcard/myfile.ext路径,模式/.*/.*/myfile\\.ext有效。如果您不确定子目录级别,则必须添加<data>具有不同值的多个元素pathPattern

<data
    android:scheme="file"
    android:mimeType="*/*"
    android:host="*" />

<data android:pathPattern="/.*/.*/.*/myfile\\.ext" /> <!-- matches file:///mnt/sdcard/tmp/myfile.ext -->

<data android:pathPattern="/.*/.*/myfile\\.ext" /> <!-- matches file:///mnt/sdcard/myfile.ext -->
Run Code Online (Sandbox Code Playgroud)

下面是用于模式匹配的PatternMatcher.matchPattern方法:

   static boolean matchPattern(String pattern, String match, int type) {
        if (match == null) return false;
        if (type == PATTERN_LITERAL) {
            return pattern.equals(match);
        } if (type == PATTERN_PREFIX) {
            return match.startsWith(pattern);
        } else if (type != PATTERN_SIMPLE_GLOB) {
            return false;
        }

        final int NP = pattern.length();
        if (NP <= 0) {
            return match.length() <= 0;
        }
        final int NM = match.length();
        int ip = 0, im = 0;
        char nextChar = pattern.charAt(0);
        while ((ip<NP) && (im<NM)) {
            char c = nextChar;
            ip++;
            nextChar = ip < NP ? pattern.charAt(ip) : 0;
            final boolean escaped = (c == '\\');
            if (escaped) {
                c = nextChar;
                ip++;
                nextChar = ip < NP ? pattern.charAt(ip) : 0;
            }
            if (nextChar == '*') {
                if (!escaped && c == '.') {
                    if (ip >= (NP-1)) {
                        // at the end with a pattern match, so
                        // all is good without checking!
                        return true;
                   }
                    ip++;
                    nextChar = pattern.charAt(ip);
                    // Consume everything until the next character in the
                    // pattern is found.
                    if (nextChar == '\\') {
                        ip++;
                        nextChar = ip < NP ? pattern.charAt(ip) : 0;
                    }
                    do {
                        if (match.charAt(im) == nextChar) {
                            break;
                        }
                        im++;
                    } while (im < NM);
                    if (im == NM) {
                        // Whoops, the next character in the pattern didn't
                        // exist in the match.
                        return false;
                    }
                    ip++;
                    nextChar = ip < NP ? pattern.charAt(ip) : 0;
                    im++;
                } else {
                    // Consume only characters matching the one before '*'.
                    do {
                        if (match.charAt(im) != c) {
                            break;
                        }
                        im++;
                    } while (im < NM);
                    ip++;
                    nextChar = ip < NP ? pattern.charAt(ip) : 0;
                }
            } else {
                if (c != '.' && match.charAt(im) != c) return false;
                im++;
            }
        }

        if (ip >= NP && im >= NM) {
            // Reached the end of both strings, all is good!
            return true;
        }

        // One last check: we may have finished the match string, but still
        // have a '.*' at the end of the pattern, which should still count
        // as a match.
        if (ip == NP-2 && pattern.charAt(ip) == '.'
            && pattern.charAt(ip+1) == '*') {
            return true;
        }

        return false;
    }
Run Code Online (Sandbox Code Playgroud)