3

my Activity class calls to another non-activity class and when i try to use openFileOutput, my IDE tells me that openFileOutput is undefined. please help:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;

import android.util.Log;
import android.content.Context;

public class testFile(){

Context fileContext;

public testFile(Context fileContext){
    this.fileContext = fileContext;
}

public void writeFile(){
    try{
            FileOutputStream os = fileContext.getApplicationContext().openFileOutput(fileLoc, Context.MODE_PRIVATE);
        os.write(inventoryHeap.getBytes()); // writes the bytes
        os.close();
        System.out.println("Created file\n");
    }catch(IOException e){
        System.out.print("Write Exception\n");
    }
}
}
ddan
  • 295
  • 1
  • 5
  • 17

4 Answers4

2

You've already got a Context.

FileOutputStream os = fileContext.openFileOutput(fileLoc, Context.MODE_PRIVATE);
Brian Dupuis
  • 8,136
  • 3
  • 25
  • 29
0

I'm writing this more for me than for anybody else. I'm new to android programming. I had the same problem and I fixed by passing the context as a parameter to the method. In my case the class was trying to write to a file using a piece of code that I found in a Java example. Since I just wanted to write the persistence of an object and didn't wanted to concern myself with the "where" the file was, I modified to the following:

public static void Test(Context fileContext) {
  Employee e = new Employee();
  e.setName("Joe");
  e.setAddress("Main Street, Joeville");
  e.setTitle("Title.PROJECT_MANAGER");
  String filename = "employee.ser";
  FileOutputStream fileOut =  fileContext.openFileOutput(filename, Activity.MODE_PRIVATE); // instead of:=> new FileOutputStream(filename);
  ObjectOutputStream out = new ObjectOutputStream(fileOut);
  out.writeObject(e);
  out.close();
  fileOut.close();
}

and from the calling activity I use the following:

SerializableEmployee.Test(this.getApplicationContext());

Worked like a charm. Then I could read it with (a simplified version):

public static String Test(Context fileContext) {
  Employee e = new Employee();
  String filename = "employee.ser";
  File f = new File(filename);
  if (f.isFile()) {
    FileInputStream fileIn = fileContext.openFileInput(filename);// instead of:=> new FileInputStream(filename);
    ObjectInputStream in = new ObjectInputStream(fileIn);
    e = (Employee) in.readObject();
    in.close();
    fileIn.close();
   }
  return e.toString();
}
Alex Alvarez
  • 371
  • 3
  • 6
0

I deleted my answer from before, since I was wrong, the problem I see is that you add () to the class declaration: public class testFile(){. it should be public class testFile{. That's all.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • thanks for the help. your previous answer was correct. i just typed my example wrong, that's why i had public class testFile(){ – ddan May 04 '11 at 21:24
  • This is funny you say so, since using your exact example, but only with the fix I wrote in this answer, I have no errors... – MByD May 04 '11 at 21:56
  • ok... i just did that. i had the non-activity class extend my activity class. and i took out the getContextApplication() in my non-activity class. i also changed the type of fileContext to Activity. – ddan May 05 '11 at 11:15
-1

You might try changing Context fileContext; to static Context fileContext;

eaavendano
  • 77
  • 7