Thursday, March 8, 2012

Permission to write to the SD card


I would like my app to archive the application DB to the SD card. In my code I check if the directory canWrite(), and if not then throw an IOException. In this perticular instance, I am trying to copy the db file to the root directory on the SD card, but its throwing an IOException. How can I change the permission on a folder/file to be able to write to it?



thanks



patrick

1 comment:

  1. You're right that the SD Card directory is /sdcard but you shouldn't be hard coding it. Instead, make a call to Environment.getExternalStorageDirectory() to get the directory:

    File sdDir = Environment.getExternalStorageDirectory();


    If you haven't done so already, you will need to give your app the correct permission to write to the SD Card by adding the line below to your Manifest:

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

    ReplyDelete