I'm trying to write a file using the FileOutputStream
and OutputStreamWriter
but I keep getting a read only warning.
Here is the code that I am using
FileOutputStream fw = new FileOutputStream((trackName.replaceAll(" ", ""))+".gpx", true);
OutputStreamWriter osw = new OutputStreamWriter(fw);
osw.write(XML_HEADER+"\n");
osw.write(TAG_GPX+"\n");
writeTrackPoints(trackName, osw, locations, time, trackDescp);
writeWayPoints(osw, locations,time);
osw.flush();
osw.close();
And the Logcat
output is
java.io.FileNotFoundException: /test.gpx (Read-only file system)
at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
at com.fyp.run_race.GPXFileWriter.<init>(GPXFileWriter.java:25)
at com.fyp.run_race.GPSTrackDetails$1.onClick(GPSTrackDetails.java:58)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
I ament sure what the problem is really.
Dunno why Pratik's comment was downvoted -- he's right in that you'll want the permission to write to external storage. Assuming that's where you want to write.
Devconsole is also right; you can't just write to any arbitrary filename, most of the file system is locked down and read-only.
To write to an arbitrary location in external storage (sdcard), use getExternalStorageDirectory() for the root of external storage. Also use getExternalStorageState() to confirm that external storage is actually available.
Don't place files in the root of external storage; place them in a subdirectory to prevent the root from becoming too cluttered. (Edit: and some Android implementations won't let you do this anyway.)
Use getExternalFilesDir() to get a location in external storage that's nominally private to your application. (New in API 8)
Use getFilesDir() to get a location under /data/ where you can write application-private files. This isn't on external storage, so space is tight; don't write big files here.
See also:
You can write files only to Internal Storage (device memory) or External Storage (the SD card), see http://developer.android.com/guide/topics/data/data-storage.html. Simply opening a FileOutputStream with an arbitrary file name won't work since that file system is read-only.
just give permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With