简体   繁体   English

Android文件写入只读文件系统警告

[英]Android file writing Read-Only file system warning

I'm trying to write a file using the FileOutputStream and OutputStreamWriter but I keep getting a read only warning. 我正在尝试使用FileOutputStreamOutputStreamWriter编写文件,但我一直收到只读警告。

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 Logcat输出是

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. Dunno为什么Pratik的评论被低估了 - 他是对的,因为你需要获得写入外部存储的许可。 Assuming that's where you want to write. 假设你想写的地方。

Devconsole is also right; Devconsole也是对的; 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. 要写入外部存储(sdcard)中的任意位置, 请将getExternalStorageDirectory()用于外部存储的根目录。 Also use getExternalStorageState() to confirm that external storage is actually available. 还可以使用getExternalStorageState()确认外部存储实际可用。

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.) (编辑:有些Android实现不会让你这样做。)

Use getExternalFilesDir() to get a location in external storage that's nominally private to your application. 使用getExternalFilesDir()获取外部存储中的位置,该位置名义上是您的应用程序专用的。 (New in API 8) (API 8中的新功能)

Use getFilesDir() to get a location under /data/ where you can write application-private files. 使用getFilesDir()获取/ data /下的位置,您可以在其中编写应用程序专用文件。 This isn't on external storage, so space is tight; 这不是外部存储,因此空间紧张; don't write big files here. 不要在这里写大文件。

See also: 也可以看看:

  • getDir() -- Get a directory under /data/data/ yourapname / getDir() - 获取/ data / data / yourapname /下的目录
  • getFilesDir() -- Return the directory /data/data/ yourappname /files/ getFilesDir() - 返回目录/ data / data / yourappname / files /
  • fileList() -- List the files in /data/data/ yourappname /files/ fileList() - 列出/ data / data / yourappname / files /中的文件
  • getFileStreamPath() -- Get a file in /data/data/ yourappname /files/ getFileStreamPath() - 获取/ data / data / yourappname / files /中的文件
  • openFileInput() -- Opens a file from within /data/data/ yourappname /files/ for reading openFileInput() - 从/ data / data / yourappname / files /中打开文件进行读取
  • openFileOutput() -- Opens a file from within /data/data/ yourappname /files/ for writing openFileOutput() - 从/ data / data / yourappname / files /中打开文件进行写入
  • getCacheDir() -- Return the directory /data/data/ yourappname /cache/ getCacheDir() - 返回目录/ data / data / yourappname / cache /
  • getExternalCacheDir() -- Return the directory /sdcard/Android/data/ yourappname /cache/ getExternalCacheDir() - 返回目录/ sdcard / Android / data / yourappname / cache /
  • deleteFile() 删除文件()

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 . 您只能将文件写入内部存储(设备内存)或外部存储(SD卡),请参阅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. 简单地打开具有任意文件名的FileOutputStream是行不通的,因为该文件系统是只读的。

只需在清单中给予许可

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM