在尝试将文件写入 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();
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>
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)
我在真实设备上运行它,而不是 avd。我尝试过重新启动我的设备并重建项目。 OfficeSuite 等应用程序可以毫无问题地写入此位置。
设备:配备 Android 2.3.7 (CyanogenMod 7) 的 ZTE Blade
尝试这个:
File file = new File(Helpers.StripExtension(filePath) + ".blk");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(digest.getBytes());
fos.close();
如果这不起作用,我认为您在读取原始文件时可能会遇到问题(digest.getBytes()
)。在此发布更多代码,我们可以从那里开始
Edit:
如果它不允许您写入所选目录,请尝试使用如下路径进行测试:
File file = new File(Environment.getExternalStorageDirectory() + "test_file.blk");
它应该可以正常工作,并且您可能只是没有对您正在使用的目录的写入权限。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)