android:pathPattern为单个文件

时间:2014-09-02 09:20:03

标签: android android-manifest mime-types intentfilter

我需要为名为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>

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

任何线索?

1 个答案:

答案 0 :(得分:2)

Android模式细节:

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

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

.*尝试匹配任何字符,直到第一次出现m,这恰好是第二个字符,即/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 -->

以下是用于模式匹配的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;
    }