6

我正在做一个新的安卓应用程序。我想在 sdcard 中的“Android”文件夹中创建一个文件夹。在此之前,我想检查该文件夹是否具有读/写权限。我怎么能得到那个?谁能帮我做到这一点。

4

2 回答 2

16

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

File f = new File("path/to/dir/or/file");
if(f.canWrite()) {
    // hell yeah :)
}
于 2013-02-10T12:34:00.117 回答
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" />
于 2013-02-10T13:30:58.580 回答