即使权限设置和外部存储处于MEDIA_MOUNTED状态,也无法写入SD卡

时间:2012-10-25 17:02:48

标签: android io

在尝试将文件写入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的中兴Blade(CyanogenMod 7)

3 个答案:

答案 0 :(得分:0)

试试这个:

File file = new File(Helpers.StripExtension(filePath) + ".blk");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(digest.getBytes());
fos.close();

如果这不起作用,我认为您在阅读原始文件(digest.getBytes())时可能会遇到问题。在此发布更多代码,我们可以从那里开始

编辑:

如果不允许您写入所选目录,请尝试使用以下路径进行测试:

File file = new File(Environment.getExternalStorageDirectory() + "test_file.blk");

它应该可以正常工作,你可能只是没有对你正在使用的目录的写权限。

答案 1 :(得分:0)

试试这个:

File file = new File(filePath);
File parent = file.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
      return;
}

file = new File(Helpers.StripExtension(filePath) + ".blk");
OutputStream fos = new FileOutputStream(file);
fos.write(digest.getBytes());
fos.close();

答案 2 :(得分:0)

太好了,前段时间我遇到了同样的问题。缺少路径分隔符(或其所谓的路径分隔符)。

File file = new File(Environment.getExternalStorageDirectory() + "test_file.blk");

没有用,但是

File file = new File(Environment.getExternalStorageDirectory() + "/test_file.blk");

表现良好

此外:

FileWriter f = new FileWriter(Environment.getExternalStorageDirectory().getPath()+ "/foobar");
f.append("stuff");
f.close();

工作正常等。
我认为异常说它是readonly,因为根文件系统确实是只读的。

注意:
java.io.FileNotFoundException: /foobar (Read-only file system)表示我们正在写入系统根目录 java.io.FileNotFoundException: /mnt/sdcard/foobar (Read-only file system)表示sdcard root(我没有得到这个例外,这只是一个例子)

tl; dr 错误路径