我需要为名为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"
等等 - 但它仍然无法处理我的文件。
任何线索?
答案 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;
}