Problema al escribir en archivos memoria externa en Android

84 views
Skip to first unread message

Leandro Spadaro

unread,
Sep 26, 2011, 12:15:53 PM9/26/11
to Java android, Grupo Java J2ME JAVA
Buenas a todos,
 
Viendo que no puedo escribir en una base de datos SqlLite en una memoria externa decidi escribir y leer de archivos txt en la
memoria externa y luego pasar de los archivos a la base SqlLite que tiene android. Para lo cual estoy probando con un ejemplo
en donde tengo un DAO que se encuentran los metodos de apertura, escritura y cierre del archivo.
El problema que tengo es que crea el archivo pero no escribe en el archivo el texto de prueba.
 
Alguien me podria dar una mano.
 
gracias
Leandro.
 
Adjunto las clases.
 
 
/******************** Activity  **********************/
 
public class PruebaSD extends Activity {
    
 private Button btnEscribirSD = null;
 private Button btnLeerSD = null;
 private EditText txtResultado = null;
 
 //Variable para probar DAO
 private DAO dao = new DAO;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnEscribirSD = (Button)findViewById(R.id.BtnEscribirSD);
        btnLeerSD = (Button)findViewById(R.id.BtnLeerSD);
        txtResultado = (EditText)findViewById(R.id.TxtResultado);
        
                
        btnEscribirSD.setOnClickListener(new OnClickListener() {
   
         @Override
         public void onClick(View arg0) { 
      
           try{
  
              dao.escribirArchivoMemoriaExterna("prueba_sd.txt", "ejemplo de escritura");
    
          }catch(Exception e){
            Log.e("Ficheros", e.getMessage()); 
          }
        }
       });
        
   }
}
 
 
 
/***************** DAO **********************/ 
 

public

class DAO{

 

private FileReader reader;

private BufferedReader input;

private BufferedWriter writer;

private PrintWriter print;

private String registroArchivo;

private File path;

private File file;

 

public

void escribirArchivoMemoriaExterna (String archivo, String registro)throws DAOException {

 try {

  abrirArchivoMemoriaExternaParaEscritura(archivo);

  escribirArchivoMemoriaExterna(registro);

  cerrarArchivoParaEscrituraMemoriaExterna(archivo);

 }

catch (DAOException ux) {

   throw ux;

 }

catch (Exception ex) {

   throw new DAOException("Error de sistema");

 }

}

 

public void abrirArchivoMemoriaExternaParaEscritura(String archivo) throws DAOException {

 try {

  boolean sdDisponible = false;

  boolean sdAccesoEscritura = false;

 

  String estado = Environment.getExternalStorageState();

 

  if (estado.equals(Environment.MEDIA_MOUNTED)){

    sdDisponible = true;

    sdAccesoEscritura = true;

  }else if (estado.equals(Environment.MEDIA_MOUNTED_READ_ONLY)){

    sdDisponible = true;

    sdAccesoEscritura = false;

  }else {

    sdDisponible = false;

    sdAccesoEscritura = false;

  }

 

  if (sdDisponible && sdAccesoEscritura){

   path= Environment.getExternalStorageDirectory();

   file= new File(path.getAbsolutePath(), archivo);

   writer= new BufferedWriter(new FileWriter(file));

   print = new PrintWriter(writer);

  }else{

   throw new DAOException("No se puede abrir, para escritura, el archivo " + archivo);

  }

 

 }catch (Exception ex){

  throw new DAOException("Error de sistema");

 }

}

 

 

private boolean escribirArchivoMemoriaExterna(String registro) throws DAOException {

 

 try {

  print.println(registro);

 } catch (Exception ex) {

  throw new DAOException("Error al escribir en el archivo");

 }

}

 

public void cerrarArchivoParaLecturaMemoriaExterna(String archivo) throws DAOException {

 

 try {

   if (input != null) {

     input.close();

   }

 } catch (IOException e) {

  throw new DAOException("No se puede cerrar el archivo " + archivo);

 }

}

 

public void cerrarArchivoParaEscrituraMemoriaExterna(String archivo) throws DAOException {

 

 try {

  if (input != null) {

   print.close();

  }

 } catch (Exception e) {

  throw new DAOException("No se puede cerrar el archivo " + archivo);

 }

}

 

}

 

Carlos Rafael Ramirez

unread,
Sep 26, 2011, 1:03:37 PM9/26/11
to solo...@googlegroups.com
Estás preguntando por input en vez de por print. No cierra el archivo por ende no vacía el buffer y lo más probable es que no escriba:

--
Has recibido este mensaje porque estás suscrito al grupo "soloJ2ME" de Grupos de Google.
Para publicar una entrada en este grupo, envía un correo electrónico a solo...@googlegroups.com.
Para anular tu suscripción a este grupo, envía un correo electrónico a soloj2me+u...@googlegroups.com
Para tener acceso a más opciones, visita el grupo en http://groups.google.com/group/soloj2me?hl=es.

Leandro Spadaro

unread,
Sep 26, 2011, 1:35:17 PM9/26/11
to Grupo Java J2ME JAVA, Java android
Tenes mucha razón, ese era el problema.
muchas garcias.
Leo
 

Date: Mon, 26 Sep 2011 12:03:37 -0500
Subject: Re: [soloJ2ME] Problema al escribir en archivos memoria externa en Android
From: crra...@gmail.com
To: solo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages