Android read/write permission of a folder

风流意气都作罢 提交于 2019-12-07 05:13:27

问题


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.


回答1:


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 :)
}



回答2:


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" />


来源:https://stackoverflow.com/questions/14797746/android-read-write-permission-of-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!