简体   繁体   中英

Dealing with Read-only file system while trying to write to SD-card

I try to write some data to SD-card but as you can see I have used TOAST and I get this msg when I press addbtn : "Read only file system" and it's obvious that it doesn't write to sd-card so how should I solve this? thanks in advance here's the code I used:

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;

here's my LogCat stack:

> 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

Change:

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

to:

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

Then, change:

 File myFile = new File(s);

to:

 File myFile = new File(mydir, s);

Maybe the code you have is trying to write inside / which is read only . If you want to store the file inside SD card, you have to change to :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM