2013-02-10 156 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" />