miércoles, 9 de noviembre de 2011

Acceso a la tarjeta SD


File directorioSD = Environment.getExternalStorageDirectory();

public void onCreate(Bundle savedInstanceState)
{
 super.onCreate(savedInstanceState);
 // para mostar resultados
 TextView textView = new TextView(this);
 setContentView(textView);
 // para ver si la SD esta montada
 String state = Environment.getExternalStorageState();
 if (!state.equals(Environment.MEDIA_MOUNTED))
 {
    textView.setText("No external storage mounted");
 }
 else
 {
    // para ver el directorio de la SD que usaremos
    File externalDir = Environment.getExternalStorageDirectory();
        // creamos un fichero nuevo para probar
    File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt");
    try
      {
           //escribimos en el fichero de texto
      writeTextFile(textFile, "This is a test. Roger");
            //leemos del fichero de texto
      String text = readTextFile(textFile);
            //mostramos el texto en el view
      textView.setText(text);
      if (!textFile.delete())
          {
              //borramos el fichero
        textView.setText("Couldn't remove temporary file");
      }
    } 
    catch (IOException e)
    {
      textView.setText("something went wrong! " + e.getMessage());
  }
 }
}


No hay comentarios:

Publicar un comentario