1

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
Erfan
  • 163
  • 1
  • 8

2 Answers2

0

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);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • didn't solve my problem, this time I get this msg : No such file or directory(I guess that's because I can't write to mem and it's read-only – Erfan Oct 15 '13 at 14:33
  • @user2541758: Paste your entire stack trace into your question. – CommonsWare Oct 15 '13 at 14:36
  • @user2541758: That is not a stack trace. – CommonsWare Oct 15 '13 at 14:53
  • how should i get stack trace then?sorry I'm newbie – Erfan Oct 15 '13 at 14:56
  • @user2541758: It looks like you are not actually logging it. You are catching the `Exception` and then just displaying a `Toast`. Use `Log.e()` to log the `Exception` and its stack trace to LogCat. – CommonsWare Oct 15 '13 at 15:19
  • I just used it, you can see it now – Erfan Oct 15 '13 at 16:10
  • @user2541758: In the future, use the three-parameter version of `Log.e()`, that takes your `Exception` as the third parameter, to generate an actual stack trace. Your problem is that you are trying to open a file named simply `"Erfan"`, which will not work, as it is not a fully-qualified path. If you follow the instructions in my answer, you will get a fully-qualified path. – CommonsWare Oct 15 '13 at 16:11
0

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);
Seraphim's
  • 12,559
  • 20
  • 88
  • 129