Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method OpenFileOutput() is undefined !

firstly, I coded some methods in Main Activity, But I decided they should be a class.

this is my code... openFileOutput and openFileInput are undefined. Any idea?? maybe it should be service or activity...??

    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;
}

}

like image 503
atasoyh Avatar asked Oct 25 '10 14:10

atasoyh


2 Answers

Those are methods defined on the Context class, not methods defined in your class. When your code was part of an Activity, it could use a convenience method openFileInput() in its Activity base class to access the underlying Context.getApplicationContext().openFileInput() (and similarly for openFileOutput()).

Now you'll have to replace those with the direct calls to the underlying Context methods.

like image 109
Pontus Gagge Avatar answered Sep 24 '22 00:09

Pontus Gagge


Replace

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

with below line

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

If used inside Fragment

FileOutputStream fos =getActivity().openFileOutput(filename, getActivity().MODE_PRIVATE);
like image 41
Antesh Sharma Avatar answered Sep 21 '22 00:09

Antesh Sharma