尝试写入SD卡时处理只读文件系统

时间:2013-10-15 14:23:26

标签: android android-sdcard

我尝试将一些数据写入SD卡,但正如您所看到的,我已经使用了TOAST,当我按下addbtn时,我得到了这个消息: “只读文件系统” 很明显,它不会写入SD卡 那我该怎么解决呢? 提前致谢 这是我使用的代码:

case R.id.donebtn:
    if (subject.getText().toString().isEmpty()) {
        startActivity(new Intent(this, emptysbj.class));
    }
    else {
        String s = subject.getText().toString();
        String n = note.getText().toString();
        try {
            File mydir = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "myTasks" + File.separator + s);
        mydir.mkdirs();
        File myFile = new File(s);
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter =
                new OutputStreamWriter(fOut);
        myOutWriter.append(n);
        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Done writing to SD :" + s,
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }

}
finish();
break;

这是我的LogCat堆栈:

> 10-15 19:36:27.195: I/[POST_RESELECT](8750): [spanChange] (o,
> oldStart, newStart, oldEnd,
> newEnd)=(android.text.Selection$START@401041e0,-1,0,-1,0) 10-15
> 19:36:27.195: I/[POST_RESELECT](8750): [spanChange] (o, oldStart,
> newStart, oldEnd,
> newEnd)=(android.text.Selection$END@401402b8,-1,0,-1,0) 10-15
> 19:36:27.195: I/[POST_RESELECT](8750): [spanChange] (o, oldStart,
> newStart, oldEnd,
> newEnd)=(android.text.Selection$START@401041e0,-1,0,-1,0) 10-15
> 19:36:27.195: I/[POST_RESELECT](8750): [spanChange] (o, oldStart,
> newStart, oldEnd,
> newEnd)=(android.text.Selection$END@401402b8,-1,0,-1,0) 10-15
> 19:36:27.205: I/[POST_RESELECT](8750): [spanChange] (o, oldStart,
> newStart, oldEnd,
> newEnd)=(android.text.Selection$START@401041e0,-1,0,-1,0) 10-15
> 19:36:27.205: I/[POST_RESELECT](8750): [spanChange] (o, oldStart,
> newStart, oldEnd,
> newEnd)=(android.text.Selection$END@401402b8,-1,0,-1,0) 10-15
> 19:36:29.067: I/[POST_RESELECT](8750):
> [sendCursorChangeNotificationToIME] ENTER... 10-15 19:36:29.067:
> I/[POST_RESELECT](8750): NOW IS XXX NOT COMPOSING..... 10-15
> 19:36:29.067: I/[POST_RESELECT](8750): [getWordOnCursor] cursor_pos=0
> 10-15 19:36:29.067: I/[POST_RESELECT](8750):
> [sendCursorChangeNotificationToIME] TAP..... 10-15 19:36:29.067:
> I/[POST_RESELECT](8750):
> [sendCursorChangeNotificationToIME](content,cursor_start,tap)=(,0,false)
> 10-15 19:36:29.617: I/[POST_RESELECT](8750): [handleTextChanged]
> (start,before,after)=(0,0,1) 10-15 19:36:29.817:
> I/[POST_RESELECT](8750): [handleTextChanged]
> (start,before,after)=(1,0,1) 10-15 19:36:29.998:
> I/[POST_RESELECT](8750): [handleTextChanged]
> (start,before,after)=(2,0,1) 10-15 19:36:31.209: E/No such file or
> directory(8750): Erfan

2 个答案:

答案 0 :(得分:0)

变化:

 File mydir = new File(Environment.getExternalStorageDirectory() +File.separator+
                            "myTasks"+File.separator+s);

为:

 File mydir = new File(Environment.getExternalStorageDirectory(), "myTasks");

然后,改变:

 File myFile = new File(s);

为:

 File myFile = new File(mydir, s);

答案 1 :(得分:0)

也许你的代码试图在 /内写,只读。如果要将文件存储在SD卡中,则必须更改为:

File root = Environment.getExternalStorageDirectory();
File file = new File(root, "myTasks" + File.separator + s);