please format code with </> button * homework policy * asking questions
I’m converting a sketch for saving text files on android and running into the cannot find symbol error when compiling in eclipse.
original sketch
class fileOutput {
PrintWriter output;
boolean save, onMouseUp, mdown, debug, append, appendFile, match,append2,overWrite;
int counter, counter2;
File file;
File[] SDcards ;
String location, filePath,folderPath = "";
PApplet applet;
Activity activity ;
Context context ;
String dataPath = "data";
fileOutput() {
appendFile = true;
};
fileOutput(boolean a) {
overWrite = true;
appendFile = true;
};
fileOutput(PApplet applet,String location) {
//appendFile = true;
this.applet = applet;
this.location = location;
//Activity activity = applet.getActivity();
//Context context = activity.getApplicationContext();
//SDcards = context.getExternalFilesDirs(null);
checkLocation(location);
open();
//dataPath = dataPath(dataPath);
//println(dataPath);
};
//currently unused
fileOutput(String l, boolean m) {
appendFile = true;
location = l;
checkLocation(location);
//dataPath = dataPath(dataPath);
//println(dataPath);
//open();
//file = dataFile(location);
//filePath = file.getPath();
};
fileOutput(PApplet p,String l, boolean m) {
appendFile = m;
applet = p;
location = l;
//Activity activity = applet.getActivity();
//Context context = activity.getApplicationContext();
//SDcards = context.getExternalFilesDirs(null);
checkLocation(location);
open();
//dataPath = dataPath("");
println("test",folderPath +counter+"/"+ location);
checkFiles(folderPath.substring(0,folderPath.lastIndexOf("/")));
checkFiles(folderPath+counter);
checkFiles(folderPath+counter+"/"+location);
//check();
String []s1 = listNames(dataPath(""));
if(s1!=null){
println("11",s1.length,dataPath(""));
for(int i=0;i<s1.length;i++){
println(i,"1", s1[i]);
}}
String []s2 = listNames(dataPath("")+"/folder");
if(s2!=null){
println("22",s2.length,dataPath("")+"/folder");
for(int i=0;i<s2.length;i++){
println(i,"2", s2[i]);
}}
String []s3 = listNames(dataPath("")+"/folder/"+counter);
if(s3!=null){
println("33",s3.length,dataPath("")+"/folder/"+counter);
for(int i=0;i<s3.length;i++){
println(i,"3", s3[i]);
}}
String []s4 = listNames(folderPath);
if(s4!=null){
println("44",s4.length,folderPath);
for(int i=0;i<s4.length;i++){
println(i,"4", s4[i]);
}}
//String []s5 = listNames(dataPath("")+"/folder/"+counter);
//if(s5!=null){
// for(int i=0;i<s5.length;i++){
// println("i", s5[i]);
//}}
//println("context",SDcards[0]);
//String []s6 = loadStrings(folderPath+counter+"/"+location);
//println("fileLocation" ,folderPath+counter+"/"+location);
//write("hello");
//write("hello");
//write("hello");
//write("hello");
//if(s6!=null){
// for(int i=0;i<s6.length;i++){
// println("i","6", s6[i]);
//}}else println("null");
//println("context",SDcards[0]);
};
void checkLocation(String location){
int count = 0;
folderPath = dataPath("")+"/" +folderPath;
for(int i=location.length()-1;i>-1;i--){
char c = location.charAt(i);
if(c=='/'){
folderPath = location.substring(0,i);
this.location = location.substring(i,location.length());
count ++;
break;
}
}
if(count==0)this.location = location;
folderPath = dataPath("")+"/"+folderPath;
this.location = this.location.replace("/","");
this.location = this.location.replace(folderPath,"");
String s1 = folderPath;
String s2 = this.location.replace("/","");
println("folder",folderPath);
println("file",location);
println("checkLocation:",s1 + counter + "/" + s2);
};
void update(String folder, String file,int counter ){
//filePath = folder + "\\" + file;
this.folderPath = folder ;
this.location = file;
this.counter = counter;
appendFile = false;
overWrite = true;
};
void saveData() {
if (mdown()) {
checkFile( location, append);
}
if (mdown)
output.println(mouseX + ",+ " + mouseY);
close();
};
void open() {
checkFile(location, append);
};
void write(String s) {
//function to write without overwriting or append
if(output!=null)
{
println("success",s);
output.println(s);
output.flush();
output.close();
}
else print("Create Save File");
};
void write(float f) {
output.println(str(f));
};
void write_(String s) {
//function to overwrite file
if(output!=null){
output.print(s);
println("success",s);
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
}else println("failed",location,folderPath);
};
void write(String []s) {
String s1 = "";
for (int i=0; i<s.length; i++) {
s1 += s[i];
}
if(s1!=null&&output!=null)output.println(s1);
};
void close() {
if(output!=null){
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
}else println(location,folderPath);
};
boolean onMouseUp() {
boolean k = false;
if (pos()&&!mousePressed&&onMouseUp) {
onMouseUp = false;
} else if (pos()&&mousePressed&&!onMouseUp) {
output.println(counter);
onMouseUp = true;
k = false;
} else if (onMouseUp&&!mousePressed) {
k = true;
onMouseUp = false;
counter ++;
}
return k;
};
boolean mdown() {
boolean k= false;
if (mdown)k = false;
if (mousePressed&&!mdown) {
mdown = true;
k = true;
}
if (!mousePressed)mdown = false;
return k;
};
boolean pos() {
return mouseX>0&&mouseX<width&&mouseY>0&&mouseY<height;
};
void checkFile(String location, boolean append) {
if (appendFile) {
file = dataFile(folderPath + "/" + location);
filePath = file.getPath();
filePath = filePath.replace(folderPath, "");
String[] list = null;
if(debug)println("checkFile");
//println(filePath);
if (listNames(filePath)!=null&&!match) {
list = listNames(filePath);
boolean b = false;
for(int j=maxFolderSize;j>-1;j--){
//println(j);
if(b)break;
counter = j;
for (int i=list.length-1;i>-1; i--) {
int n = int(list[i]);
if (j == n){
//println(j + ", " + list[i]);
counter = j;
b = true;
break;
}}}
match = true;
if(!b)counter = -1;
if(counter>=0)counter = counter + 1;
else counter = 0;
//println(counter);
}
else{
file = dataFile(folderPath + "/" + location);
filePath = file.getPath();
filePath = filePath.replace(folderPath, "");
}
file = dataFile(folderPath + "/" + location);
filePath = file.getPath();
appendFile = false;
append = true;
}
if(file!=null&&(!file.exists()&&!append2)) {
//output = createWriter("/data/" + folderPath + "/" + counter + location);
//println("folderpath " + folderPath + "\\" + counter+"\\" + location);
}
if(appendFile||output == null){
//output = createWriter(folderPath +"/"+counter+"/"+ location);
println("new folderpath " + folderPath );
println("fileName", location);
file = dataFile(folderPath +location);
filePath = file.getPath();
filePath = filePath.replace(location, "");
folderPath = filePath;
println( filePath);
}
if (debug) println(filePath);
try {
FileWriter fw = new FileWriter(file, true);///true = append
BufferedWriter bw = new BufferedWriter(fw);
output = new PrintWriter(bw);
println("success fw",folderPath);
println("location",location);
}
catch(IOException ioe) {
System.out.println("Exception ");
ioe.printStackTrace();
println(filePath);
}
};
void check() {
if (appendFile) {
//file = dataFile(folderPath + "/" + location);
//filePath = file.getPath();
//filePath = filePath.replace(folderPath, "");
String[] list = null;
println("check",folderPath);
println(listNames(folderPath));
if (listNames(folderPath)!=null&&!match) {
list = listNames(folderPath);
println(list);
boolean b = false;
for(int j=maxFolderSize;j>-1;j--){
//println(j);
if(b)break;
counter = j;
for (int i=list.length-1;i>-1; i--) {
int n = int(list[i]);
if (j == n){
//println(j + ", " + list[i]);
counter = j;
b = true;
break;
}}}
match = true;
if(!b)counter = -1;
if(counter>=0)counter = counter + 1;
else counter = 0;
//println(counter);
}
else{
file = dataFile(folderPath + "/" + location);
filePath = file.getPath();
filePath = filePath.replace(folderPath, "");
}
file = dataFile(folderPath + "/" + counter +"/"+ location);
filePath = file.getPath();
appendFile = false;
append = true;
}
if(file!=null&&(!file.exists()&&!append2)) {
//output = createWriter("/data/" + folderPath + "/" + counter + location);
//println("folderpath " + folderPath + "\\" + counter+"\\" + location);
}
if(true){
output = createWriter(folderPath + "/" + counter + "/"+ location);
println("new folderpath " + folderPath + counter+"/");
println( location);
file = dataFile(folderPath + "/" + counter + "/" + location);
filePath = file.getPath();
filePath = filePath.replace(location, "");
folderPath = filePath;
println( filePath);
}
if (debug) println(filePath);
try {
FileWriter fw = new FileWriter(file, true);///true = append
BufferedWriter bw = new BufferedWriter(fw);
output = new PrintWriter(bw);
println("success fw",folderPath);
println("location",location);
}
catch(IOException ioe) {
System.out.println("Exception ");
ioe.printStackTrace();
println(filePath);
}
};
};
void checkFiles(String s){
try{
String dirName = s;
File newFile = new File(s);
if(newFile.exists() && newFile.isDirectory()) {
println("Directory Exists... All Good",s);
}
else {
println("Directory Doesn't Exist... We're Making It",s);
if(newFile.isDirectory())newFile.mkdirs();
else newFile.mkdir();
}
}
catch(Exception e) {
println(e);
}
};
String[] listNames(String dir) {
if(dir==null)return null;
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
};
int totalFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names.length;
} else {
// If it's not a directory
return -1;
}
};
String getFileExtension(File file) {
String fileName = file.getName();
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
return fileName.substring(fileName.lastIndexOf(".")+1);
else return null;
};
it works perfectly well in processing but cannot be compiled in eclipse. Now I understand that the processing pde once in android mode will probably make use of a custom lib in the background which it doesnt visibly declare.
when compililing I get 8 errors all folling the same vein
c] C:\Users\paul goux\Desktop\androidGui-master\tmp\androidGui\src\androidGui\fileOutput.java:70: error: cannot find symbol
[javac] String s = Environment.getExternalStorageDirectory().getAbsolutePath();
I have added the android processing core to the build path played around with shrinked android from the sdk folder as initially processing core didnt have the necessary classes to complete the import statements and yet the result fails every time.
I’m not knowledgeable enough about android or eclipse to know what I’m doing wrong, I understand it may be a build.properties problem and again I have added the jars in the properties class path, but no luck.
Any help would be appreciated as I have ran into this problem before and makes me think that the android package is not designed to be used in certain ways, ie google would prefer if people used android studio.
If this is the case then I suppose this is the final push to help to decide to dig into android studio with a bit more seriousness.