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



>我尝试将一些数据写入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

更改:

 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);

也许您拥有的代码正在尝试在只读/内部编写如果要将文件存储在SD卡中,则必须更改为:

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

最新更新