在做文件管理器开发时,遇到这样的一个问题,如果文件的命名比较特殊:[1] 文件名含有空格 [2] 文件中是汉字。在intent接收方处理时,应稍做转换,否则会出现错误。Intent发送方要想正确传递文件的uri也应该做getUri(file)的处理.
转换的函数如下:
File file = getFile(intent.getData());
Uri uri = getUri(file);
public static File getFile(Uri uri) {
if (uri != null) {
String filepath = uri.getPath();
if (filepath != null) {
return new File(filepath);
}
}
return null;
}
public static Uri getUri(File file) {
if (file != null) {
return Uri.fromFile(file);
}
return null;
}