File contains a path separator


#1

Hello,
I have a problem, that was listed many times on forum two and even here. But my problem is a bit different. I don’t want to save or load anything from the data folder. Instead I want to load a file from anywhere on the storage.
I get the exception
java.lang.IllegalArgumentException: File filepath contains a path separator.


#2

@FurryNightShade===
difficult to help you without knowing where are supposed to be your files: standard folders from Android (“download”, “pictures”…), on the phone, on the sdcard…; difficult also to help without seeing the code you are using for retrieving them, though, as you say yourself,this error was often posted and solved here or in the previous forum and means (in 2 words) that a FILE is not a path (STRING) && that you ll never see a file called “/C/USERS/DOCUMENTS/MY FOLDER/myFile.jpg” - Anyway you have probably to use, from Android, the “getExternalPublicStorageDirectory()” method or its sister “getExternalStorageDirectory()”, first one for standard folders, second one for others places.


#3

Well, I’m working on a better version of my audio visualizer.
Before it could read only the file song.mp3 in the dataPath("") folder. Now, I want the user to select a song using a file explorer and since the selectInput() is not working on android, I want to create a file explorer of my own inside that program. Also, when using selectInput() on Windows, all files show up, not just the ones I want.
I want to access ANY path, that’s publicly available, ie. /sdcard/folder/path.format.
I’m at the stage, where I need only one file for testing purposes: /sdcard/Sketchbook/VisualizerProject/data/song.mp3, but I still have no way to access it, as dataPath("") folder is empty even though the projects’ data folder has the song.mp3 file in it.
Also, I don’t mind, if it’s imposible, but it would be nice to have a solution compatible with Windows, as I’m going to make a Windows version as well.


#4

@FurryNightShade ====

there are many ways to do what you want (as for android); in my mind the most easy is to use the “specific folders” from android (“camera”, “pictures”…and so on); then, knowing that all your songs are inside the needed folder you create an alertDialog with a list and an adapter with a on clickListener(): that gives you exactly the same result than a select input.
— as for “exploring” the WHOLE phone i think that there is no “simple” way to to that: code will be different if you are looking for a file which stays in SDCARD or in internal files or in external files, yet i dont understand your problem: put your files here or here and then code function of this choice;
if you dont, you have to create (again) an alertDialog which asks to the user what to do (“looking here???”, or “here” and so on till you have explored all possible places…
But begin with the more simple way (supposing that you are not asking that the user can choose ONLY from the data or assets folder, which seems to me that you are trying:from all the files - songs- on the phone)

  • as for the other problem(android , windows…) that is simple: you have to write 2 codes and select what to execute according to the platform.

#5

Here’s a simple example:

import processing.sound.*;
void setup(){
  SoundFile sf = new SoundFile(this, "/sdcard/Personal Stuff/Outside Music/song.mp3");
  sf.play();
}
void draw(){background(0);}

Now, I need a way to point to that file, which could anywhere on the storage space.


#6

@FurryNightShade ====

your code has nothing to do with “select input”, then i cannot understand yourquestion and that s why i have answered with “alertDialog”; if you know which is the file && where it is and dont leave any choice to the user that is another problem which depends of WHERE is this file and the way for android to write code for that; i can write code for SD Card (1) is it what you want? Have you tryed what said (“begin by the most simple way”) and putting your file(s) into the android specific folder for sounds?

ANDROID MAKES EASY WHAT ANDROID DOES NATIVELY (OS)
WHEN YOU TAKE A PHOTO WITH YOUR PHONE WHERE IS IT PUT?

  • what i mean is: i am very surprised when people ask for “coding” and seem never having thought what the phone is doing each minute by itsellf…

#7

What I need is a way to point to any file in the storage. I also need a workng select input.
The code is just an example of a path I need.


#8

I think that it would make sense for you to write it on your own. Make the start path being String startPath = dataPath(""), so to get a list of all files there you do new File(startPath).listFiles();, which will give you a File[] array, on which you can iterate with functions like stuff[i].isDirectory() to know what is what, and then make a dialog box with a scrollable list of buttons for every single thing in the current directory - with an additional button to go up a directory.


#9

@Architector_4 yes, I can do that. But when I choose the file, I’ll basically have a string again and I can’t access the file using the string.


#10

Well, I don’t know, how I did it, but the folowing code works:

import processing.sound.*;

String filePath = "/sdcard/Sketchbook/VisualizerProject/data/song.mp3";

void setup(){
  stealFile(filePath, "song.mp3");
  SoundFile sf = new SoundFile(this, /*dataPath("song.mp3")*/filePath);
  sf.play();
}

void draw(){
  background(0);
}

boolean stealFile(String from, String filename){
  println("Stealing file \"" + from + "\"...");
  if(!(new File(from).exists())){
    println("   Fail: Input path invalid...");
    return false;
  }
  BufferedReader reader;
  PrintWriter writer;
  try{reader = createReader(from);}
  catch(Exception e){
    println("   Fail: Reader couldn't be created...");
    e.printStackTrace();
    return false;
  }
  try{writer = createWriter(dataPath(filename));}
  catch(Exception e){
    println("   Fail: Writer couldn't be created...");
    e.printStackTrace();
    return false;
  }
  
  while(true){
    String l;
    try{l = reader.readLine();}
    catch(Exception e){continue;}
    if(l == null) break;
    writer.println(l);
  }
  
  try{
    reader.close();
  }
  catch(Exception e){
    println("Reader couldn't be closed");
  }
  writer.flush();
  writer.close();
  
  println("    Success: File \"" + from + "\" succesfilly stolen and copied as \"" + filename + "\"."); 
  return true;
}

#11

@FurryNightShade===

yes, but this code does not answer to your question because you KNOW a) you hardcode the path b) the user cannot choose nothing; if you want to see all songs on your phone, try someting like that::

import android.os.Environment;


void setup(){
 size(800,1000);
 orientation(PORTRAIT);
 
  File f= Environment.getExternalStorageDirectory();//dossier de base
  parcoursDossier(f);
  
}

void draw(){
  
}
public void parcoursDossier(File dossier) {

        File[] listFile;///Array de tous les fichiers
        listFile = dossier.listFiles();//remplir l'array

        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {//recursive call if it s a directory
                    parcoursDossier(listFile[i]);
                } else {
                  if (listFile[i].getName().toLowerCase().endsWith(".mp3")){///you can change .mp3 for other suff
                      
                      String nom = listFile[i].getName();
                      println("nom du fichier====" + nom);
                      println(listFile[i].getAbsolutePath());
                      
                      ///then you can read this file or do stuff with it: ocreate an arrayList, open an alertDialog and an adapter with your arrayList and leave the user choose!
                  }
                }
            }
        } else{
          
          println("je ne trouve rien");
        }
    }

#12

@akenaton, My original question might have been badly stated. I wanted to access any file in any directory, but I always got that error.

Listing all songs is just recursively going through the file system and returning all files with supported format. I’ve done that before.

My program is not a player, but an editor of some sort. You choose one song only and that’s the base of your project. Would it be better to list all songs or to code in a file explorer?


#13

@FurryNightShade===

What I need is a way to point to any file in the storage. I also need a workng select input.

My code lists all songs and give you the way to open an alertDialog() (see my comments)
So what is the problem now?