我正在尝试使用 openFileOutput 函数,但它无法编译并且无法识别该函数。我正在使用安卓 SDK 1.6。这是一个sdk问题吗?这是参数问题吗?
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();
}
}
最佳答案
你的方法应该如下。接受一个额外的上下文作为参数。您可以将您的服务或 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();
}
}
关于java - android openFileOutput有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3625837/