Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android read/write permission of a folder

I am doing a new android app. I want to create a folder in "Android" folder which is available in sdcard. Before that I want to check whether the folder has read/write permission. How can I get that? can anyone help me to do this.

like image 206
Manoj Avatar asked Feb 10 '13 12:02

Manoj


People also ask

What does permission 644 and 755 mean for a file?

755 - owner can read/write/execute, group/others can read/execute. 644 - owner can read/write, group/others can read only.

How do you get read and write permissions in Android?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.


2 Answers

You do it the old-school java way. Create a file object and call canWrite() and canRead().

File f = new File("path/to/dir/or/file");
if(f.canWrite()) {
    // hell yeah :)
}
like image 180
poitroae Avatar answered Sep 20 '22 13:09

poitroae


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

 File path = getExternalFilesDir();

It will be your own directory so if you have permission for this, you will be able to read/write it if the external storage is available. To check 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;
}

Permissions required to write:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 30
EMarci15 Avatar answered Sep 20 '22 13:09

EMarci15