在尝试将文件写入SD卡时,我得到了java.io.FileNotFoundException: /filename (Read-only file system)异常.可悲的是,这里发布的众多解决方案都没有帮助我.我已设置权限,外部存储处于MEDIA_MOUNTED状态.这是有问题的代码:
Log.i("STORAGE", Environment.getExternalStorageState());
OutputStream fos = new FileOutputStream(Helpers.StripExtension(filePath) + ".blk");
fos.write(digest.getBytes());
fos.close();
Run Code Online (Sandbox Code Playgroud)
filePath来自文件自动收报机.我要做的是从所选文件中读取,然后保存另一个具有相同名称,不同扩展名和其中一些内容的文件.
表现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mypackagename"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@android:style/Theme.Black" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
logcat的:
10-25 18:24:34.330: I/FILEPATH(1989): /mnt/sdcard/Untitled.txt
10-25 18:24:34.360: I/STORAGE(1989): mounted
10-25 18:24:34.360: W/System.err(1989): java.io.FileNotFoundException: /mnt/sdcard/Untitled.blk (Read-only file system)
10-25 18:24:34.360: W/System.err(1989): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
10-25 18:24:34.360: W/System.err(1989): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
10-25 18:24:34.360: W/System.err(1989): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
10-25 18:24:34.360: W/System.err(1989): at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
10-25 18:24:34.360: W/System.err(1989): at java.io.FileOutputStream.<init>(FileOutputStream.java:144)
Run Code Online (Sandbox Code Playgroud)
我在真正的设备上运行它,而不是avd.我尝试重新启动设备并重建项目.OfficeSuite等应用程序可以毫无问题地写入此位置.
设备:采用Android 2.3.7的中兴Blade(CyanogenMod 7)
太好了,我前段时间也遇到过同样的问题。路径分隔符(或任何名称)丢失。
File file = new File(Environment.getExternalStorageDirectory() + "test_file.blk");
Run Code Online (Sandbox Code Playgroud)
没用,但是
File file = new File(Environment.getExternalStorageDirectory() + "/test_file.blk");
Run Code Online (Sandbox Code Playgroud)
表现良好
还:
FileWriter f = new FileWriter(Environment.getExternalStorageDirectory().getPath()+ "/foobar");
f.append("stuff");
f.close();
Run Code Online (Sandbox Code Playgroud)
工作正常等。
我认为例外表示它是只读的,因为根文件系统确实是只读的。
注意:
java.io.FileNotFoundException: /foobar (Read-only file system)意味着我们正在写入系统根
java.io.FileNotFoundException: /mnt/sdcard/foobar (Read-only file system)意味着SD卡根(我没有得到这个异常,这只是一个例子)
tl;博士错误的道路