File contains a path separator.

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

When I try to check the existence of a particular file, I get java.lang.illegalArgumentException: File contains a path separator

What is the right way to do this using 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);} 

回答1:

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



回答2:

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.

public static Boolean fileExist(Activity activity , String filePath) {         String finalPath = activity.getFilesDir().toString() + File.separator + filePath;         File file = new File(finalPath);         return file.exists();     } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!