Android 文件包含路径分隔符。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11467359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File contains a path separator.
提问by Jason O.
When I try to check the existence of a particular file, I get java.lang.illegalArgumentException: File contains a path separator
当我尝试检查特定文件是否存在时,我得到 java.lang.illegalArgumentException: File contains a path separator
What is the right way to do this using getFileStreamPath(..)?
使用 getFileStreamPath(..) 执行此操作的正确方法是什么?
File file = getActivity().getFileStreamPath("mnt/sdcard/photo/1342147146535.jpg");
if(file.exists()){
Toast.makeText(getActivity(), "File exists in /mnt", Toast.LENGTH_SHORT);
}
I also tried the following to replace the first line of the above codes. None of these worked.
我还尝试了以下替换上述代码的第一行。这些都没有奏效。
File file = getActivity().getFileStreamPath("file:///mnt/sdcard/photo/aviary_1342147146535.jpg");
File file = getActivity().getFileStreamPath("/mnt/sdcard/photo/1342147146535.jpg");
// File file = getActivity().getFileStreamPath("mnt/sdcard/photo/1342147146535.jpg");
// File file = getActivity().getFileStreamPath("file:///mnt/sdcard/photo/1342147146535.jpg");
if(file.exists()){
Toast.makeText(getActivity(), "File exists in /mnt", Toast.LENGTH_SHORT);}
else {
Toast.makeText(getActivity(), "File NOT exists in /mnt", Toast.LENGTH_SHORT);}
采纳答案by Dipak Keshariya
回答by user1154390
I had same problem when I was trying with method getFileStreamPath. I think it takes file as parameter not whole path that why it through exception. Isolved with following method.
我在尝试使用 getFileStreamPath 方法时遇到了同样的问题。我认为它将文件作为参数而不是整个路径,这就是为什么它通过异常。用以下方法解决。
public static Boolean fileExist(Activity activity , String filePath) {
String finalPath = activity.getFilesDir().toString() + File.separator + filePath;
File file = new File(finalPath);
return file.exists();
}