方法 OpenFileOutput() 未定义!
-
26-09-2019 - |
题
首先,我在主活动中编写了一些方法,但我认为它们应该是一个类。
这是我的代码...openFileOutput 和 openFileInput 未定义。任何想法??也许应该是服务或活动......?
package spexco.hus.system;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import spexco.hus.cepvizyon.CepVizyon;
import android.content.Context;
public class LicenseIDB {
private String PHONECODEFILE = "CepVizyonCode";
private static String PhoneCode = null;
public LicenseIDB() {
if (readLocal(PHONECODEFILE, 8) == null)
createSystemCode();
}
public static long getDate() {
Date currentTime = new Date();
return currentTime.getTime();
}
public void createSystemCode() {
long date = getDate();
String str = Integer.toHexString(Integer.MAX_VALUE - (int) date);
for (int i = str.length(); i < 8; i++) {
str += "" + i;
}
PhoneCode = str.substring(0, 8);
saveLocal(PhoneCode, PHONECODEFILE);
}
public static String getPhoneCode() {
return PhoneCode;
}
public void saveLocal(String fileString, String Adress) {
try {
FileOutputStream fos = openFileOutput(Adress, Context.MODE_PRIVATE);
fos.write(fileString.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readLocal(String Adress, int lenght) {
byte[] buffer = new byte[lenght];
String str = new String();
try {
FileInputStream fis = openFileInput(Adress);
fis.read(buffer);
fis.close();
str = new String(buffer);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}
解决方案
这些是定义在 Context
类,而不是类中定义的方法。当你的代码是 活动, ,它可以使用一种方便的方法 打开文件输入() 在其 Activity
访问底层的基类 Context.getApplicationContext().openFileInput() (同样对于 openFileOutput()
).
现在您必须将它们替换为对底层的直接调用 语境 方法。
其他提示
替换
FileOutputStream fos = openFileOutput(Adress, Context.MODE_PRIVATE);
低于线
FileOutputStream fos = getApplicationContext().openFileOutput(filename, getActivity().MODE_PRIVATE);
如果内部片段使用
FileOutputStream fos =getActivity().openFileOutput(filename, getActivity().MODE_PRIVATE);
不隶属于 StackOverflow