Android SD card writing, Permission Denied

user329604 picture user329604 · Dec 22, 2010 · Viewed 64.3k times · Source

I am trying to write a file to SDCard with below Code (permission android.permission.WRITE_EXTERNAL_STORAGE already set in manifest.xml). Upon execution of nmea_file.createNewFile(); it throws exception with Permission Denied.

Any guesses why would this be happening?

if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
  {
     Log.d(TAG, "Sdcard was not mounted !!" ); 
  }
else
  {
    File nmea_file; 
    File root = Environment.getExternalStorageDirectory();
    FileWriter nmea_writer = null;
    try {
        nmea_file = new File(root,"NMEA.txt");
        if(!nmea_file.exists()) {
                Log.w(TAG, "File Doesn't Exists!");
                nmea_file.createNewFile();
            }
        nmea_writer = new FileWriter(nmea_file);
        nmea_writer.append(nmea);
        nmea_writer.flush();
    }
    catch (IOException e) 
    {
        Log.w(TAG, "Unable to write", e);
    } 
    finally 
    {
        if (nmea_writer != null) 
        {
            try 
            {
                nmea_writer.close();
            } 
            catch (IOException e) 
            {
                Log.w(TAG, "Exception closing file", e);
            }
        }
    }
  }

Answer

Butsaty picture Butsaty · Dec 8, 2011

Add to manifest.xml

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