FileWriter exception

  • 9 Antworten
  • Letztes Antwortdatum
P

philips0815

Neues Mitglied
0
Hallo!

Bekomme ständig eine Exception wenn ich eine Datei schreiben will.
Habe schon mehrere Methoden versucht jedoch immer das gleiche.


Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SettingsActivity [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity {
EditText [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ipadr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]port[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
Button [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btnsave[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]file[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] String([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"config.ini"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ipadresse[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"MyDebug:"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]protected[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onCreate(Bundle savedInstanceState) {
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].onCreate(savedInstanceState);
setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]activity_settings[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ipadr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (EditText)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]ip_adresse[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]port[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (EditText)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]port_nummer[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btnsave[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = (Button)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btn_save[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);

}

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onClick(View v){

FileWriter fWriter;
FileOutputStream fos;

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {
fos = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileOutputStream([/SIZE][U][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"/sdcard/test.txt"[/U][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]true[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {
fWriter = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileWriter(fos.getFD());
fWriter.write([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"hi"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
fWriter.close();
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Exception e) {
Log.[I]d[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Exeption"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]finally[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {
fos.getFD().sync();
fos.close();
}
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Exception e) {
Log.[I]d[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Exeption"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
}



finish();
startActivity([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Intent (SettingsActivity.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], MainActivity.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));

}[/LEFT]
}
[/SIZE]

danke an alle!!
mfg
 
A

Anzeige

  • Gerade eben
  • Neu
Wie lautet denn die Fehlermeldung? LogCat?

Möglicherweise ist auf deinem Gerät der externe Speicher nicht unter /sdcard, nimm lieber

Code:
Environment.getExternalStorageDirectory().getAbsolutePath()
 
Hallo!
Danke für deine Antwort!

Im Logcat bekomme ich keine Fehler nur nach meinem Log.d(TAG, String) Exception habe ich diese Einträge:
01-02 03:18:33.140: D/MyDebug:(15907): /mnt/sdcard/test.txt
01-02 03:18:33.140: D/MyDebug:(15907): FileNotFoundException

Den Code habe ich geändert jedoch das selbe Problem.
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onClick(View v){

FileWriter fWriter;
FileOutputStream fos;
String file = Environment.[I]getExternalStorageDirectory[/I]().getAbsolutePath() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"/test.txt"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
Log.[I]d[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], file);


[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {

fos = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileOutputStream(file, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]true[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
fWriter = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileWriter(fos.getFD());
fWriter.write([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"hi"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
fWriter.close();
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (FileNotFoundException e) {
Log.[I]d[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"FileNotFoundException"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
e.printStackTrace();
} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (IOException e) {
Log.[I]d[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"IOException"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
e.printStackTrace();
}






}[/LEFT]

[/SIZE]
 
Aha, es ist also schonmal /mnt/sdcard und nicht /sdcard.

Dann würde ich mal den Kram mit dem Filedescriptor weglassen. Evtl ist der noch nicht verfügbar, wenn eine neue Datei geschrieben wird?

Du kannst eigentlich direkt einen FileWriter aufmachen, also

Code:
PrintWriter out = new PrintWriter( new FileWriter(
  new File( [SIZE=2]Environment.[I]getExternalStorageDirectory[/I]([/SIZE]), "test.txt" )
));
out.println( "hallo welt" );
out.close();
Dein Exception handling ist auch suboptimal, weil du den Stacktrace nicht ordentlich ausgibst. Pack ihn doch in den Log mit rein, also Log.d(TAG,"file schreiben geht nicht",e); bzw gleich Log.e(...) nehmen, ist ja ein error :D
 
Zuletzt bearbeitet:
Installier die App auf dein Smartphone, stöpsel es ab(!!) und dann starte die App ... Stürzt sie noch ab? Wenn du das Ding angeschlossen hast, dann kann es sein, dass deine SD-Kare (am PC) gemountet wird und für das Handy nicht mehr verfügbar (schreiben + lesen) ist (je nachdem welcher Hersteller, ob es eine richtige SD-Karte oder eine emulierte ist, ...)

Man sollte zuvor checken ob der Speicher verfügbar ist ...
Storage Options | Android Developers
 
Danke! :D
Das war wohl das Problem.
Funktioniert es wenn ich in den Einstellungen im Handy nur laden aktiviere?
Laut externalsdcard State habe ich Write und Read rechte.
mit angeschlossenem USB kann ich debug Meldungen ausgeben was ungemein nützlich ist.
 
Ja "nur laden" sollte funktionieren.

Ich hab bspw. ein "neueres" Gerät, eins ohne "richtige" SD-Karte, sondern nur noch mit einer "simulierten" Karte, weswegen das Ding am PC eh nur noch per MTP/HTC Sync/Android File Transfer erkannt wird und nicht als normaler Wechseldatenträger... deswegen muss ich nichts umstellen :) und kann sowohl vom PC aus zugreifen, als auch gleichzeitig eine App drauf schreiben/lesen lassen (und mir gleichzeitig ADB-Meldungen ausgeben lassen)
 
Funktioniert nun nicht schlecht>>Danke!

Grund war 1. das die SD Card bei angestecktem USB am Pc gemounted war, und 2. es auch die Datei schon gab und nicht darauf zugegriffen werden konnte und es auch deshalb zur FileNotFound Exception kam.

Kann ich auch irgend wo auf der internen Sd Card einen Ordner und Datei erstellen worauf ich ganz normal zugreifen kann?
 
Wenn du dir den von mir geposteten Link etwas genauer anschauen würdest (im Sinne von: Nach oben scrollen), dann würdest du hier landen: Storage Options | Android Developers

;)
 
Ja genau, Sorry!!
Hat anscheinend mein Mausrad geklemmt um rauf zu scrollen.:lol:

Nochmals Danke!!
 
Zurück
Oben Unten