Question

I have a main activity, and when pressing on a listItem, It launches a new activity by an intent. This new activity's theme is Dialog (so it looks like the second activity is floating over the main activity).

In this new activity I enter some data in EditTexts, then with that data I create a custom object, and try to save it in internal storage.

it seems it isn't creating the file. I can't really understand why it isn't working, as I am using the same structure with other objects in other activities and it's working properly. This is what makes me think that it must have something to do with the fact that this activity's theme is Dialog. The custom object class obviously implements Serializable, and I have all the permissions in my android manifest. Maybe the problem is related to the context I'm passing to the saving method(getApplicationContext()).

This is the Activity with the Dialog theme:

package es.lagoacunado.lagoacunadoconsutores;

import java.io.File;

import utilidades.SeriaPreferencias;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import clases.Preferencia;

public class Preferencias extends Activity {

    EditText ftpDir, ftpUser, ftpPass;
    Button btnGuardar, btnCancelar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferencias);

        this.setFinishOnTouchOutside(false);

        ftpDir = (EditText) findViewById(R.id.ftpDIR);
        ftpUser = (EditText) findViewById(R.id.ftpUSER);
        ftpPass = (EditText) findViewById(R.id.ftpPASS);
        btnGuardar = (Button) findViewById(R.id.btnGuardar);
        btnCancelar = (Button) findViewById(R.id.btnCancelar);

        File filePref = new File("preferencias.dat");
        if (filePref.exists()) {
            /*
             * ftpDir.setText(SeriaPreferencias.cargarPreferencias(
             * getApplicationContext()).getDirFTP());
             * ftpUser.setText(SeriaPreferencias.cargarPreferencias(
             * getApplicationContext()).getUsrFTP());
             * ftpPass.setText(SeriaPreferencias.cargarPreferencias(
             * getApplicationContext()).getPssFTP());
             */
            Preferencia loadPref = SeriaPreferencias
                    .cargarPreferencias(getApplicationContext());
            ftpDir.setText(loadPref.getDirFTP());
            ftpUser.setText(loadPref.getUsrFTP());
            ftpPass.setText(loadPref.getPssFTP());
        }

        btnGuardar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String dir, usr, pss;
                dir = ftpDir.getText().toString();
                usr = ftpUser.getText().toString();
                pss = ftpPass.getText().toString();

                //Log.d("PRUEBA Guardar Preferencias", "dir= "+dir+" -- user= "+usr+" -- pss= "+pss);

                Preferencia pref = new Preferencia(dir, usr, pss);
                // METHOD THAT SAVES THE OBJECT pref
                SeriaPreferencias.guardarPreferencias(getApplicationContext(),
                        pref);

                File prueba = new File("preferencias.dat");
                if(prueba.exists()){
                    Log.d("-->PRUEBA", "EL ARCHIVO EXISTE");
                }

                finish();
            }
        });

        btnCancelar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}

This is the class with the saving and loading methods:

package utilidades;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;
import android.util.Log;
import clases.Preferencia;

public class SeriaPreferencias {

    private static String FICHERO = "preferencias.dat";

    public static void guardarPreferencias(Context context, Preferencia prefs) {

        try {
            FileOutputStream fos = context.openFileOutput(FICHERO,
                    Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(prefs);

            oos.close();
            fos.close();

        } catch (Exception e) {
            Log.e("MiCV", e.getMessage(), e);
        }
    }

    public static Preferencia cargarPreferencias(Context context) {
        Preferencia prefs = null;

        try {

            FileInputStream fis = context.openFileInput(FICHERO);
            ObjectInputStream ois = new ObjectInputStream(fis);

            prefs = (Preferencia) ois.readObject();

            ois.close();
            fis.close();

        } catch (Exception e) {
            Log.e("MiCV", e.getMessage(), e);
        }

        return prefs;
    }

}

and this is the custom object class:

package clases;

import java.io.Serializable;

public class Preferencia implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String dirFTP, usrFTP, pssFTP;

    public Preferencia() {

    }

    public Preferencia(String dirFTP, String usrFTP, String pssFTP) {
        this.dirFTP = dirFTP;
        this.usrFTP = usrFTP;
        this.pssFTP = pssFTP;
    }

    public String getDirFTP() {
        return dirFTP;
    }

    public void setDirFTP(String dirFTP) {
        this.dirFTP = dirFTP;
    }

    public String getUsrFTP() {
        return usrFTP;
    }

    public void setUsrFTP(String usrFTP) {
        this.usrFTP = usrFTP;
    }

    public String getPssFTP() {
        return pssFTP;
    }

    public void setPssFTP(String pssFTP) {
        this.pssFTP = pssFTP;
    }

}
Was it helpful?

Solution

 File filePref = new File("preferencias.dat");

Specifying only a filename and no directory will refer to a file in the current working directory - but this is quite likely the filesystem root directory which is not only an inappropriate place, but not writable by your application.

When you use Context.openFileOuput(filename) to open or create a FileOutputStream, the filename refers to a location in your app's private directory of the internal storage.

To create a file Object pointing towards that same file in private storage area of your application, from within an Activity class or other valid Context, during or subsequent to its onCreate() method you can use:

File filePref = new File(getFilesDir(), "preferencias.dat");

Because Context.getFilesDir() refers to the same private folder location as Context.openFileOutput() uses.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top