Warum wird nicht in die Datei geschrieben ? Kann da mal jemand drüber schauen ?

  • Antworten:2
Dennis S.
  • Forum-Beiträge: 2

06.10.2013, 21:42:22 via Website

Hey,

ich versuche gerade die GPS Daten meines Handys auf einen Server zu laden, dazu ruf ich die Daten ab und schreibe sie in eine Textdatei. Die wollte ich dann hochladen. Aber irgendwie wollen die Daten nicht in die Datei, kann mir bitte jemand sagen wieso nicht ? Es geht um diesen Code Abschnitt, unten ist der gesamte Code zu sehen

1void printSaveLocation(Location l) {
2
3 textLat.setText(String.valueOf(l.getLatitude()));
4 textLong.setText(String.valueOf(l.getLongitude()));
5 textTime.setText(String.valueOf(l.getTime()));
6
7
8 //HIER FUNKTIONIERT WAS NICHT ->
9
10 try {
11 bOut.write((String.valueOf(String.valueOf(textID.getText())+"|"+l.getTime()) + "|" + String.valueOf(l.getLatitude()) + "|" + String.valueOf(l.getLongitude())).getBytes());
12 textSend.setText("Datei geschrieben");
13 } catch (IOException e) {
14 // TODO Auto-generated catch block
15 e.printStackTrace();
16 textSend.setText("Datei nicht egeschriebent");
17 }
18
19 }

Die Textfelder zu setzen funktioniert noch, dann versuch ich die Daten in die Textdatei zu schreiben !-! Das funktioniert eben nicht !-! Dann wird angezeigt 'Datei geschrieben'...... also kommt er in den try Block....

Laut Eclipse Emulator und dessen Filesystem Browser wird die Datei erzeugt, ich kann sie öffnen aber dann ist sie leer....

Danke schonmal

Hier nochmal der ganze Code....


1package dns.example.gps;
2
3import java.io.BufferedOutputStream;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.sql.Date;
8
9import android.app.Activity;
10import android.content.Context;
11import android.location.Location;
12import android.location.LocationListener;
13import android.location.LocationManager;
14import android.os.Bundle;
15import android.widget.TextView;
16import android.provider.Settings.Secure;
17
18
19public class Main extends Activity {
20
21 TextView textLat;
22 TextView textLong;
23 TextView textTime;
24 TextView textID;
25 TextView textSend;
26 FileOutputStream fOut;
27 BufferedOutputStream bOut;
28
29
30
31 @Override
32 protected void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.activity_main);
35
36
37 textLat = (TextView)findViewById(R.id.textLat);
38 textLong = (TextView)findViewById(R.id.textLong);
39 textTime = (TextView)findViewById(R.id.textTime);
40 textID = (TextView)findViewById(R.id.textID);
41 textSend = (TextView)findViewById(R.id.textSend);
42
43 String android_id = Secure.getString(getBaseContext().getContentResolver(),
44 Secure.ANDROID_ID);
45
46 LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
47 try {
48 bOut = new BufferedOutputStream(openFileOutput(android_id, MODE_PRIVATE));
49
50 } catch (FileNotFoundException e) {
51 e.printStackTrace();
52
53 }
54 try {
55 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,ll);
56 } catch (Exception e) {
57 e.printStackTrace();
58 }
59
60 Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
61 if (l != null) {
62 printSaveLocation(l);
63 }
64
65
66
67
68 textID.setText(android_id);
69
70
71 }
72 private final LocationListener ll = new LocationListener() {
73
74 @Override
75 public void onLocationChanged(Location l) {
76
77 printSaveLocation(l);
78 }
79 @Override
80 public void onProviderDisabled(String arg0) {}
81 @Override
82 public void onProviderEnabled(String arg0) {}
83 @Override
84 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
85
86
87
88 };
89
90 void printSaveLocation(Location l) {
91
92 textLat.setText(String.valueOf(l.getLatitude()));
93 textLong.setText(String.valueOf(l.getLongitude()));
94 textTime.setText(String.valueOf(l.getTime()));
95
96
97
98 try {
99 bOut.write((String.valueOf(String.valueOf(textID.getText())+"|"+l.getTime()) + "|" + String.valueOf(l.getLatitude()) + "|" + String.valueOf(l.getLongitude())).getBytes());
100 textSend.setText("Datei geschrieben");
101 } catch (IOException e) {
102 // TODO Auto-generated catch block
103 e.printStackTrace();
104 textSend.setText("Datei nicht egeschriebent");
105 }
106
107 }
108}

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

06.10.2013, 22:04:34 via App

Glaube es fehlt ein b0ut.fush()
oder b0ut.close()

Was zeigt denn logCat?

— geändert am 06.10.2013, 22:05:10

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Dennis S.
  • Forum-Beiträge: 2

06.10.2013, 22:51:53 via Website

Hmm verdammt, stimmt. Dankee.

Antworten