0

I have to store data text to SD Card.

This is my code :

 try {

                File myFile = new File(Environment.getExternalStorageDirectory()+"/mnt/sdcard/mysdfile.txt");

                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.append(txtData.getText());
                myOutWriter.close();
                fOut.close();
                Toast.makeText(getBaseContext(),
                        "Done writing SD 'mysdfile.txt'",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }

In AndroidMainfest i have :

<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I don't understand Why it don't work ?

In Toast reported error :Permission denied?

Please help me .

Nguyen Nguyen
  • 47
  • 2
  • 9
  • try to change to: `File myFile = new File("/mnt/sdcard/mysdfile.txt");` – idiottiger Apr 01 '12 at 08:21
  • possible duplicate of [Writing a file to sdcard](http://stackoverflow.com/questions/2455102/writing-a-file-to-sdcard) – jww Feb 18 '15 at 08:27

2 Answers2

0

Try this code must solve issues...

try{
    String filename = "filename.txt";
    File myFile = new File(Environment.getExternalStorageDirectory(), filename);

    if(!myFile.exists()) 
        myFile.createNewFile();
    FileOutputStream fos;
    byte[] data = txtData.getBytes();
    try {
        fos = new FileOutputStream(myFile);
        fos.write(data);
        fos.flush();
        fos.close();
    } 
    catch (FileNotFoundException e) {
    // handle exception
  } catch (IOException e) {
    // handle exception
  }
titusfx
  • 1,896
  • 27
  • 36
Ishu
  • 5,357
  • 4
  • 16
  • 17
0

You can also try this https://github.com/uberspot/AndroidStorageUtils it's a wrapper class/package that makes storage usage in android a bit easier. :) It has a "saveStringOnExternalStorage" method as well.

cyph3r
  • 373
  • 5
  • 13