Android - Barra de progreso al escribir un archivo

 
Vista:

Barra de progreso al escribir un archivo

Publicado por Jose Montoro (1 intervención) el 28/09/2014 20:02:35
Hola, estoy intentando poner una barra de progreso en mi aplicacion para cuando escribe un archivo,

Pongo el codigo que tengo:

El cuadro de progreso:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private ProgressDialog pDialog;
private MiTareaAsincronaDialog tarea2;
 
public void dialogo() {
 
    	pDialog = new ProgressDialog(MierdaActivity.this);
		pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pDialog.setMessage("Procesando Ruta");
		pDialog.setCancelable(false);
		pDialog.setMax(100);
 
		tarea2 = new MiTareaAsincronaDialog();
		tarea2.execute();
    }

Y esto es lo que tengo para que escriba el archivo en segundo plano:

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
private class MiTareaAsincronaDialog extends AsyncTask<Void, Integer, Boolean> {
 
	@Override
	protected Boolean doInBackground(Void... params) {
 
		escribir();
 
		return true;
 
 
	}
 
	@Override
	protected void onProgressUpdate(Integer... values) {
		int progreso = values[0].intValue();
 
		pDialog.setProgress(progreso);
	}
 
	@Override
	protected void onPreExecute() {
 
		pDialog.setMax(100);
		pDialog.setProgress(0);
		pDialog.show();
 
		}
 
 
	@Override
	protected void onPostExecute(Boolean result) {
		if(result)
		{
			pDialog.dismiss();
			Toast.makeText(MierdaActivity.this, "Tarea finalizada!", Toast.LENGTH_SHORT).show();
		}
	}
 
	@Override
	protected void onCancelled() {
		Toast.makeText(MierdaActivity.this, "Tarea cancelada!", Toast.LENGTH_SHORT).show();
	}
}

Pongo tambien la funcion para escribir el archivo:

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
public void escribir () {
 
    	String cabecera = "<?xml version='1.0' encoding='UTF-8'?>\n<gpx version='1.1' " +
    			"creator='Crlive - http://www.cadaruta.es' " +
    			"xsi:schemaLocation='http://www.topografix.com/GPX/1/1 " +
    			"http://www.topografix.com/GPX/1/1/gpx.xsd " +
    			"http://www.topografix.com/GPX/Private/TopoGrafix/0/1 " +
    			"http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd' " +
    			"xmlns='http://www.topografix.com/GPX/1/1' " +
    			"xmlns:gpxtpx='http://www.garmin.com/xmlschemas/TrackPointExtension/v1' " +
    			"xmlns:gpxx='http://www.garmin.com/xmlschemas/GpxExtensions/v3' " +
    			"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
    			"\n\t<metadata>\n\t\t<name></name>\n\t\t<desc></desc>\n\t</metadata>\n\t<trk>\n\t\t<trkseg>\n";
    	String cuerpo = "";
    	String Final = "\n\t\t</trkseg>\n\t</trk>\n</gpx>";
    	double altitud = 120;
 
    	try
    	{
    		FileOutputStream fout = null;
 
    	    sdCard = Environment.getExternalStorageDirectory();
 
    	    directory = new File(sdCard.getAbsolutePath(), "/Cada Ruta/Pruebas");
    	    directory.mkdirs();
 
    	    file = new File(directory, "caca.gpx");
 
    	    fout = new FileOutputStream(file);
 
    	    OutputStreamWriter grabar = new OutputStreamWriter(fout);
 
 
			for( int i = 0 ; i < latitud.size() ; i++ ) {
				  cuerpo = cuerpo + "\t\t\t<trkpt lat='" + latitud.get(i) +
						  "' lon='"+ longitud.get(i) + "'>\n\t\t\t\t<ele>" +
						  altitud + "</ele>\n\t\t\t\t<time> + tiempoisostring[u] + " +
						  		"</time>\n\t\t\t</trkpt>\n";
				}
 
 
    	    grabar.write(cabecera + cuerpo + Final);
    	    grabar.close();
 
    	    Toast.makeText(this,"Fichero escrito con exito", Toast.LENGTH_SHORT).show();
    	}
    	catch (Exception ex)
    	{
    	    Toast.makeText(this,"Error al escribir fichero a tarjeta SD", Toast.LENGTH_SHORT).show();
    	}
}

Cualquier ayuda se agradece. 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
sin imagen de perfil

Barra de progreso al escribir un archivo

Publicado por adrian (7 intervenciones) el 28/10/2014 09:27:16
Es posible que s eejecute tan rapido que no de tiempo a visualizarla, prueba a poner algo asi cada vez que ejecutas la tarea escribir

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {

}
}, 2000); // 2 segundos de espera...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar