Writing Android files in read-only mode

I am trying to write a file using FileOutputStream and OutputStreamWriter , but I keep getting a read-only warning.

Here is the code I'm 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 Logcat output

 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 am sure the problem really is.

+4
source share
3 answers

You can write files only to internal storage (device memory) or external storage (SD card), see http://developer.android.com/guide/topics/data/data-storage.html . Just opening a FileOutputStream with an arbitrary file name will not work, as this file system is read-only.

+5
source

I donโ€™t know why Pratikโ€™s comment was blocked - itโ€™s right that you need permission to write to external storage. Assuming you want to write.

Devconsole is also right; you cannot just write an arbitrary file name; most of the file system is locked and read-only.

To record an arbitrary location in external storage (sdcard), use getExternalStorageDirectory () for the root of the external storage. Also use getExternalStorageState () to confirm that external storage is indeed available.

Do not put files in the root folder of external storage; put them in a subdirectory to prevent root overflow. (Edit: and some versions of Android will not allow you to do this anyway.)

Use getExternalFilesDir () to get the location in the external storage, which is nominally closed for your application. (New in API 8)

Use getFilesDir () to get the location under / data / where you can write personal application files. It is not on external storage, so space is limited; do not write large files here.

See also:

  • getDir () - get the directory under / data / data / yourapname /
  • getFilesDir () - returns the directory / data / data / yourappname / files /
  • fileList () - A list of files in / data / data / yourappname / files /
  • getFileStreamPath () - Get the file in / data / data / yourappname / files /
  • openFileInput () - opens a file from / data / data / yourappname / files / for reading
  • openFileOutput () - opens a file from / data / data / yourappname / files / for writing
  • getCacheDir () - returns the directory / data / data / yourappname / cache /
  • getExternalCacheDir () - returns the directory / sdcard / Android / data / yourappname / cache /
  • DeleteFile ()
+6
source

just give permission in the manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+3
source

Source: https://habr.com/ru/post/1395635/


All Articles