Data directory has no read/write permission in Android-ThrowExceptions

Exception or error:

I m using Android 1.5 my data directory doesn’t have the read/write permissions

System.out.println("DAta can write??--->"+Environment.getDataDirectory().canWrite());
System.out.println("DAta can read??--->"+Environment.getDataDirectory().canRead());

So please suggest me how to provide permissions for the data directory.

What I’m trying to do is to create a file and add some content to it in the Data storage of the emulator like as below

private void writeToSDCard() {
        try {

            File lroot = Environment.getDataDirectory();

            if (lroot.canWrite()){
                File lfile = new File(lroot, "samplefile.txt");
                FileWriter lfilewriter = new FileWriter(lfile);
                BufferedWriter lout = new BufferedWriter(lfilewriter);
                lout.write("XXXXXXXXXXXXXXXXXX");
                lout.close();
            }
        } catch (IOException e) {
            Log.e(m_cTAG, "Could not write file " + e.getMessage());
        }
    }
How to solve:

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 Reply

Your email address will not be published. Required fields are marked *