Android read and write permission in folder

I am making a new Android application. I want to create a folder in the "Android" folder, which is available in sdcard. Before that I want to check if the folder has read / write permissions. How can i get this? can anyone help me do this.

+5
source share
2 answers

You do it in the old school style. Create an object file and call canWrite()and canRead().

File f = new File("path/to/dir/or/file");
if(f.canWrite()) {
    // hell yeah :)
}
+15
source

To create a folder in the Android folder, the best way:

 File path = getExternalFilesDir();

This will be your own directory, so if you have permission to do this, you can read / write it if external storage is available. To verify this, use this code:

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}

, :

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

All Articles