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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:46:33  来源:igfitidea点击:

File contains a path separator.

androidfile

提问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

write below code for that.

为此编写下面的代码。

File file = getActivity().getFileStreamPath("/mnt/sdcard/photo/1342147146535.jpg");
if(file.exists()){
    Toast.makeText(getActivity(), "File exists in /mnt", Toast.LENGTH_SHORT);
}

And Follow below link for more detail.

并按照以下链接了解更多详情。

File Path

文件路径

回答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();
    }