Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File contains a path separator.

Tags:

file

android

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);}
like image 798
Jason O. Avatar asked Jul 13 '12 09:07

Jason O.


1 Answers

I had the same problem when I was trying with method getFileStreamPath. I think it takes file as parameter, not whole path; that's why it threw an exception. I solved 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();
    }
like image 134
user1154390 Avatar answered Oct 06 '22 00:10

user1154390