Data directory has no read/write permission in Android

You shouldn’t be looking at the Data Directory. This is a system directory in the phone’s storage – usually /data – and your application will never have permission to write to it.

The directory your application should write files to is returned by the Context.getFilesDir() method. It will be something like /data/data/com.yourdomain.YourApp/files.

If you want to write to a file in the phone’s storage use the Context.openFileOutput() method.

If you want the path to the SDCard then use Environment.getExternalStorageDirectory() method. To write to the SDCard you’ll need to give your application the appropriate permissions by adding the following to your Manifest:

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

If you’re going to write to the SDCard you’ll also need to check its state with the getExternalStorageState() method.

If you’re storing small files to do with your application then these can go into the phone’s storage and not the SD Card, so use the Context.openFileOutput() and Context.openFileInput() methods.

So in your code consider something like:

OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));

Leave a Comment