Java - Extraer informacion de archivo de texto a un TextView

 
Vista:

Extraer informacion de archivo de texto a un TextView

Publicado por Ana (1 intervención) el 19/01/2019 16:51:18
Hola a tod@s,

Les comento mi situación:

Estoy obteniendo datos por el puerto serie en Android, y quiero visualizarlos en un TextView.

He comprobado que recibo correctamente los datos mediante la Shell de Android:

cat /dev/ttyS0

131
132
133
133
134
… (se sigue actualizando)

Esos números son la temperatura que me manda el Arduino, visualizados desde la Shell Android.



Bien, pasemos a mi código JAVA:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void start(View view) throws InterruptedException {
 
    Button empezar = (Button) findViewById(R.id.empezar);
 
    Process process = null;
 
    DataOutputStream dos = null;
 
 
    try
    {
 
        process = Runtime.getRuntime().exec("su");
 
        dos = new DataOutputStream(process.getOutputStream());
        dos.writeBytes("exec 3</dev/ttyS0" + "\n"); //REDIRECT SERIAL OUTPUT TO FD 3. 
        dos.flush(); // Enter???
        dos.writeBytes("cat <&3 > /sdcard/temperatura.txt & PID=$!" + "\n"); //REDIRECT SERIAL OUTPUT TO FILE
        dos.flush();
        dos.writeBytes("kill $PID" + "\n"); //KILL CAT PROCESS
        dos.flush();
        dos.writeBytes("exec 3<&-" + "\n"); // FREE FD 3
        dos.flush();
        dos.close();
    } catch (IOException gpio) {
        gpio.printStackTrace();
    }

Con esta parte consigo crear en la memoria externa virtual (0/sdcard) un archivo temperatura.txt que se va actualizando con la información recibida por serie. Si abro temperatura.txt puedo ver los valores del serial:

131
132
133
133
134
… (se sigue actualizando temperatura.txt)





Ahora viene mi PROBLEMA:

Quiero pasar esos datos, bien desde el archivo situado en la memoria interna /dev/ttyS0 o bien desde /sdcard/temperatura.txt, a un TextView.

He probado los métodos que se describen en la web para pasar texto de un archivo a una String, pero no consigo que al presionar el botón identificado como start se muestre la información de temperatura.txt en un TextView. Al presionar el botón ocurren otros eventos que tengo programados, pero no me muestra los datos deseados en el TextView. El código completo sería:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public void start(View view) throws InterruptedException {
 
    Button empezar = (Button) findViewById(R.id.empezar);
 
    Process process = null;
 
    DataOutputStream dos = null;
 
    try
    {
 
    process = Runtime.getRuntime().exec("su");
 
    dos = new DataOutputStream(process.getOutputStream());
    dos.writeBytes("exec 3</dev/ttyS0" + "\n"); //REDIRECT SERIAL OUTPUT TO FD 3. 
    dos.flush(); // Enter???
    dos.writeBytes("cat <&3 > /sdcard/temperatura.txt & PID=$!" + "\n"); //REDIRECT SERIAL OUTPUT TO FILE
    dos.flush();
    dos.writeBytes("kill $PID" + "\n"); //KILL CAT PROCESS
    dos.flush();
    dos.writeBytes("exec 3<&-" + "\n"); // FREE FD 3
    dos.flush();
    dos.close();
   } catch (IOException gpio) {
        gpio.printStackTrace();
 
class LeeFichero {
public void main(String[] arg) {
    File archivo = null;
    FileReader fr = null;
    BufferedReader br = null;
 
    try {
        archivo = new File (Environment.getExternalStorageDirectory().getPath(), "temperatura.txt");
        fr = new FileReader (archivo);
        br = new BufferedReader(fr);
 
        // File read
        String linea = br.readLine();
        while((linea=br.readLine())!=null)
 
    // Write string to the textView    
    {
            TextView temperatura = (TextView)findViewById(R.id.temperatura);
            temperatura.setText (linea);
        }
 
    }
    catch(Exception e){
        e.printStackTrace();
    }finally{
         try{
            if( null != fr ){
                fr.close();
            }
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }
}
}



***También he probado a copiar temperatura.txt a data/data/myapk/files e introducir el siguiente código:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
try
{
    BufferedReader fin =
        new BufferedReader(
            new InputStreamReader(
                openFileInput("temperatura.txt")));
 
    String texto = fin.readLine();
    fin.close();
    TextView temp = (TextView)findViewById(R.id.temperatura);
    temp.setText((CharSequence) texto);
}
catch (Exception ex)
{
    Log.e("Ficheros", "Error al leer fichero desde memoria interna");
}
 
{
  TextView temp = (TextView) findViewById(R.id.temperatura);
    temp.setText("texto");
}



De ninguna manera consigo visualizar los datos de temperatura.txt en el TextView, por favor, alguien me puede ayudar???

Muchas gracias!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder