【问题标题】:The method OpenFileOutput() is undefined !OpenFileOutput() 方法未定义!
【发布时间】:2011-04-30 06:27:30
【问题描述】:

首先,我在 Main Activity 中编写了一些方法,但我决定它们应该是一个类。

这是我的代码... 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;
}

}

【问题讨论】:

  • @MichaelGaskill 是的,这是可能的。但是这些问题已经有 6 年的历史了..:) 我想,我在研究了很多之后才提出问题。但我不记得了,只是在想..:)

标签: android local-storage


【解决方案1】:

这些是在Context 类中定义的方法,而不是在您的类中定义的方法。当您的代码是Activity 的一部分时,它可以在其Activity 基类中使用便捷方法openFileInput() 来访问底层Context.getApplicationContext().openFileInput()(对于openFileOutput() 也是如此)。

现在您必须将这些替换为直接调用底层 Context 方法。

【讨论】:

    【解决方案2】:

    替换

    FileOutputStream fos = openFileOutput(Adress, Context.MODE_PRIVATE);
    

    下面一行

    FileOutputStream fos = getApplicationContext().openFileOutput(filename, getActivity().MODE_PRIVATE);
    

    如果在 Fragment 中使用

    FileOutputStream fos =getActivity().openFileOutput(filename, getActivity().MODE_PRIVATE);
    

    【讨论】:

    • 我在片段内做getContext().openFileOutput(filename,Context.MODE_PRIVATE),它仍然有效....我的问题是getContext()getActivity() 之间有什么区别。
    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2012-09-17
    • 2021-03-31
    • 2014-04-22
    • 2019-05-15
    • 2019-06-13
    • 2015-04-26
    相关资源
    最近更新 更多