1

I am trying to write files in the external SD card folder. Even after having set the required permission in the manifest file, I am unable to write on the external SD card.

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

Code:

String path = "/mnt/extsd/nit.txt";

    File myFile = new File(path);

    if (!myFile.exists()) {
       try {
           myFile.createNewFile();
       } catch(Exception e)
       {
           txtText.setText("Failed-" + e.getMessage());
           e.printStackTrace();
       }
    }

try {
            FileOutputStream fostream = new FileOutputStream(myFile);
            OutputStreamWriter oswriter = new OutputStreamWriter(fostream);
            BufferedWriter bwriter = new BufferedWriter(oswriter);
            bwriter.write("Hi welcome ");
            bwriter.newLine();
            bwriter.close();
            oswriter.close();
            fostream.close();
            txtText.setText("success");
        } catch(Exception e)
        {
            txtText.setText("Failed-" + e.getMessage());
            e.printStackTrace();
        }

On the other hand when I use ES File Explorer and try to create a file, it creates it without any issues.

Nitin Agarwal
  • 563
  • 1
  • 6
  • 19

3 Answers3

1

Don't use the absolute path String path = "/mnt/extsd/nit.txt"; because you never know about android device being used by users. Rather you can get the external storage directory path by using Environment.getExternalStorageDirectory().toString().

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 5
    Environment.getExternalStorageDirectory() doesn't give path of external SD card. It gives the path of internal storage which is mapped as external storage by the tab manufacturer. – Nitin Agarwal Nov 20 '14 at 10:23
0

You should be able to call Environment.getExternalStorageDirectory() to get the root path to the SD card and use that to create a FileOutputStream. From there, just use the standard java.io routines.

File log = new File(Environment.getExternalStorageDirectory(), "your_file_name.txt");
    try {
        out = new BufferedWriter(new FileWriter(log.getAbsolutePath(), false));
        out.write("any data");

    } catch (Exception e) {

    }

And don't forget to close the streams.

Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
  • 1
    Environment.getExternalStorageDirectory() doesn't give path of external SD card. It gives the path of internal storage which is mapped as external storage by the tab manufacturer. – Nitin Agarwal Nov 20 '14 at 09:57
  • So you don't want to write the file if mobile doesn't have an mounted sdcard support – Suhail Mehta Nov 20 '14 at 10:00
0

First check sd-card is available or not.

String state = Environment.getExternalStorageState();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File folder = folder = new File(extStorageDirectory, "FolderName");
if(!folder.exists())
{
    folder.mkdir();//making folder
}
File file = new File(folder,"Filename");//making file
}

Please try this code, it work in my application.

Krunal Indrodiya
  • 782
  • 2
  • 7
  • 19
  • 2
    Environment.getExternalStorageDirectory() doesn't give path of external SD card. It gives the path of internal storage which is mapped as external storage by the tab manufacturer. – Nitin Agarwal Nov 20 '14 at 09:56
  • 1
    Environment.getExternalStorageDirectory().toString() is giving me /mnt/sdcard, which is the path of internal storage. – Nitin Agarwal Nov 20 '14 at 10:31
  • Please check this [URL](http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location) – Krunal Indrodiya Nov 20 '14 at 10:37