Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android what is wrong with openFileOutput?

Tags:

java

android

I'm trying to use openFileOutput function but it doesn't compile and doesn't recognize the function. I'm using android sdk 1.6. Is this a sdk problem ? Is this a parameter problem ?

import java.io.FileOutputStream; public static void save(String filename, MyObjectClassArray[] theObjectAr) {     FileOutputStream fos;     try {         fos = openFileOutput(filename, Context.MODE_PRIVATE);          ObjectOutputStream oos = new ObjectOutputStream(fos);         oos.writeObject(theObjectAr);          oos.close();      } catch (FileNotFoundException e) {         e.printStackTrace();     }catch(IOException e){         e.printStackTrace();     } } 
like image 242
Fabien Avatar asked Sep 02 '10 09:09

Fabien


1 Answers

Your method should be as follows. Takes in an extra Context as a parameter. To this method you can pass your Service or Activity

public static void save(String filename, MyObjectClassArray[] theObjectAr,    Context ctx) {         FileOutputStream fos;         try {             fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);               ObjectOutputStream oos = new ObjectOutputStream(fos);             oos.writeObject(theObjectAr);              oos.close();         } catch (FileNotFoundException e) {             e.printStackTrace();         }catch(IOException e){             e.printStackTrace();         }     } 
like image 145
naikus Avatar answered Sep 17 '22 21:09

naikus