gpt4 book ai didi

android - 如何转义文件名中的特殊字符

转载 作者:行者123 更新时间:2023-11-29 20:51:57 29 4
gpt4 key购买 nike

我想在我的应用程序中加载一个文件并将其复制到我的应用程序文件夹中。但是,当文件名包含空格或特殊字符时,它会失败。我正在使用过滤器这是我的代码:

list

        <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.pdf" />
<data android:host="*" />
</intent-filter>
</activity>

我的 Activity

Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
if (action != null && action.compareTo(Intent.ACTION_VIEW) == 0) {
String scheme = intent.getScheme();
if (scheme.compareTo(ContentResolver.SCHEME_FILE) == 0) {
Uri uri = intent.getData();
String name = uri.getLastPathSegment();

String filepath = getPath() + name;
// file name = "test café.pdf"
}
}
}

最佳答案

我的一个应用程序中有类似的东西:

 String filepath = getPath() + removeAccents(name);

我做了这个功能,适用于所有 API:

public static String removeAccents(String s){       
try{
s = s.toLowerCase();
if(VERSION.SDK_INT > Build.VERSION_CODES.FROYO){
s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[^\\p{ASCII}]", "");
} else{
s = s.replace("á", "a").replace("é", "e").replace("í", "i").replace("ó", "o").replace("ú", "u").replace("ñ", "n");
}
}catch(Exception e){

}
return s;
}

关于android - 如何转义文件名中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28903202/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com