i want to open a file that i create in android and allways give me this error, the main problem is when i want to open the file, that the app crash
07-29 14:56:09.708: E/AndroidRuntime(577): java.lang.IllegalStateException: Could not execute method of the activity
07-29 14:56:09.708: E/AndroidRuntime(577): at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 14:56:09.708: E/AndroidRuntime(577): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:56:09.708: E/AndroidRuntime(577): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:56:09.708: E/AndroidRuntime(577): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-29 14:56:09.708: E/AndroidRuntime(577): at dalvik.system.NativeStart.main(Native Method)
07-29 14:56:09.708: E/AndroidRuntime(577): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:56:09.708: E/AndroidRuntime(577): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:56:09.708: E/AndroidRuntime(577): at menu.menuapp.FrontActivity.confirm(FrontActivity.java:51)
07-29 14:56:09.708: E/AndroidRuntime(577): at libcore.io.Posix.open(Native Method)
This is the code to save the file.
//Opening the notes file to be write.
String notes_str = "<resource></resource>"
FileOutputStream notes_file = openFileOutput("notes.xml", Context.MODE_PRIVATE);
//Converting to string the notes_doc
String notes_str = new menu.menuapp.general().xmltostring(notes_doc);
//Saving on the file
notes_file.write(notes_str.getBytes());
//Closing the file.
notes_file.close();
This is the code to open the file.
File file = new File("notes.xml");
FileInputStream notes_xml = new FileInputStream(file);
byte fileContent[] = new byte[(int)notes_xml.available()];
//The information will be content on the buffer.
notes_xml.read(fileContent);
String strContent = new String(fileContent);
notes_xml.close();
You are not specifying the path correctly when you open the file for read. The constructor you use for File requires a full path, not just a file name.
If you write the file with openFileOutput(), then I suggest you read it back with openFileInput(). That way you’re more likely to get all the parameters right.
For more guidance on reading and writing files, see here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Also, your filename is different when reading and writing. You should define that in a string resource or at least a static final.