2013-02-10 91 views
5

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

+0

的文件夹不能有读/写权限。它是一个文件夹。用户是对文件夹具有读/写权限的用户。那是你在找什么? – 2013-02-10 12:33:25

回答

15

你这样做的老派java方式。 创建一个文件对象并呼叫canWrite()canRead()

File f = new File("path/to/dir/or/file"); 
if(f.canWrite()) { 
    // hell yeah :) 
} 
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" />