Fileoutputstream fails in context

Asked

Viewed 55 times

0

I’m trying to reuse a class, which writes the file to the internal storage. In the first line of try, the result is null (from activity) and makes a mistake. When I execute this code on mainActivity, it works.

public class Modelo {
    private static Context context;

    public void saveDisciplina(String nome, Float p1, Float p2){
        FileOutputStream fileOutputStream;
        String filename=" - "+nome;

        try {
            fileOutputStream=getContext().getApplicationContext()
                    .openFileOutput(filename,getContext().MODE_PRIVATE);
            fileOutputStream.write(("Nome da disciplina: "+filename+"\n").getBytes());
            fileOutputStream.write(("Nota P1: "+p1+"\n").getBytes());
            fileOutputStream.write(("Nota P2: "+p2+"\n").getBytes());
            fileOutputStream.close();
        }catch (IOException ex){

        }
        SharedPreferences pref = this.getSharedPreferences("com.fatec.karina.mygrades", 
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("nomed", String.valueOf(nomed));
        editor.putFloat("p1", p1);
        editor.putFloat("p2", p2);
        editor.commit();

    }

    public static Context getContext (){
        return context;
    }
}
  • 4

    Static context is a bad idea. Why don’t you pass this context in the constructor?

  • 2

    Or directly in the method.

  • Okay, thanks for the help. .

2 answers

0

public void saveDisciplina(String nome, Float p1, Float p2, Context context){
     // context recebendo no parametro do método ou no construtor
     // método
}
  • Putting in the method, I had already tried, but I was in doubt when bringing the context parameter.

0


you did so o:

// ...
try {
    // ...
    fileOutputStream.close();
} catch (IOException ex){ //... }

to work, you need to do something like this:

// ...
PrintWriter writer = null;
try {
    FileOutputStream fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
    writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos)));
    writer.println(("Nome da disciplina: "+filename+"\n").getBytes());
    // ...
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (null != writer) {
        writer.close();
    }
}
// ...

Note: this is as basic an example as possible, Voce can shape from it to your needs, here with me it worked!

I hope it helps!!!

  • I created a constructor with the context. 
eo restante class
FileOutputStream fileOutputStream = context.openFileOutput(filename,Context.MODE_PRIVATE);
 writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fileOutputStream)));
 writer.println(("Name of discipline: " + filename + " n"). getBytes());
}catch (IOException ex){
 SharedPreferences preferences = context.getSharedPreferences("com.fatec.karina.mygrades",Context.MODE_PRIVATE);
 SharedPreferences.Editor editor= preferences.edit();
 editor.putString("Nomed", String.valueOf(filename));

  • It worked perfectly. Thank you to everyone who helped me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.