Android文件夹的读/写权限

时间:2013-02-10 12:30:57

标签: android android-sdcard read-write

我正在做一个新的Android应用程序。我想在“Android”文件夹中创建一个文件夹,该文件夹在sdcard中可用。在此之前,我想检查该文件夹是否具有读/写权限。我怎么能得到它?任何人都可以帮助我这样做。

2 个答案:

答案 0 :(得分:15)

你用老式的java方式做。 创建一个文件对象并调用canWrite()canRead()

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

答案 1 :(得分:6)

要在Android文件夹中创建文件夹,最好的方法是:

 File path = getExternalFilesDir();

它将是您自己的目录,因此如果您拥有此权限,则可以在外部存储可用时读取/写入该目录。要检查这个,请使用以下代码:

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