1

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

2 回答 2

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);
于 2013-10-15T14:28:42.690 回答
0

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

File root = Environment.getExternalStorageDirectory();
File file = new File(root, "myTasks" + File.separator + s);
于 2013-10-15T14:29:00.940 回答