2015-05-09 16 views
-1

我想創建MyFolder目錄,但不能做到這一點,嘗試過不同的方式,但沒有人工作; 在AndroidManifest.xml中我有:Android:mkdir無法正常工作

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="my.package" > 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="com.android.vending.BILLING" /> 

    <application 
    <!---.....--> 
</manifest> 

靜態類

public static String FILES_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyFolder"; 

代碼

File dir = new File(Constants.FILES_DIR); 
if (!dir.exists()) { 
    dir.mkdir(); 
    Log.i("my tag", "isDirectory + " + dir.toString() + " | " + dir.exists() + " | " + dir.mkdir()); 
    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MyFolder"); 
    Log.i("my tag", "isDirectory + " + dir.toString() + " | " + dir.exists() + " | " + dir.mkdir()); 
    dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "MyFolder/"); 
    Log.i("my tag", "isDirectory + " + dir.toString() + " | " + dir.exists() + " | " + dir.mkdir()); 

    Log.i("my tag", "Status + " + Environment.getExternalStorageState()); 
    if (!dir.isDirectory()) return false; 
} 

而且logcat的:

05-09 14:40:15.131 1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false 
05-09 14:40:15.131 1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false 
05-09 14:40:15.136 1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/Music/myFolder | false | false 
05-09 14:40:15.136 1453-1453/my.package I/my tag﹕ Status + mounted 
05-09 14:40:17.261 1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false 
05-09 14:40:17.261 1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/myFolder | false | false 
05-09 14:40:17.261 1453-1453/my.package I/my tag﹕ isDirectory + /storage/emulated/0/Music/myFolder | false | false 
05-09 14:40:17.266 1453-1453/my.package I/my tag﹕ Status + mounted 

我也試過mkdirs();,用斜線和無,谷歌終於it..and ,我渴望

當我手動箱文件夾,並試圖保存文件到路徑,我收到權限錯誤 - 打開失敗:EACCES(拒絕)

+0

嘗試File dir = new File(FILES_DIR); – Jamil

+0

@Softcoder FILES_DIR是一個常數,放在不同的類中,我把它放在一起。在logcat中,我們可以看到路徑是正確的 – IceJOKER

回答

1

在我的一個開源項目中,我使用以下類來獲取SD卡目錄。這只是一個簡單的幫助,它應該很容易修改爲任何其他SD卡目錄或特殊目錄。

/** 
* http://developer.android.com/training/basics/data-storage/files.html 
* 
* @param albumName 
* @return 
*/ 
public static File getAlbumStorageDir(String albumName) { 
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName); 
    if (!file.mkdirs()) { 
     //This is also the case if the directory already existed 
     Log.i("wall-splash", "Directory not created"); 
    } 
    return file; 
} 

在創建一個文件我有以下代碼:

File dir = Utils.getAlbumStorageDir("wall-splash") 
//create the file 
File file = new File(dir, fileName); 
if (!file.exists()) { 
    file.createNewFile(); 
} 

什麼設備要測試嗎?它有外部存儲嗎?任何自定義ROM?


嘗試重新啓動您的設備,以確保以前的嘗試沒有任何內容被阻止。

+0

,因爲U可以在logcat中看到 - Environment.getExternalStorageState()是MOUNTED。 / Samsung Galaxy s4 – IceJOKER

+0

@IceJOKER你也有READ_EXTERNAL_STORAGE權限嗎?編輯:忽略此評論「任何聲明WRITE_EXTERNAL_STORAGE權限的應用程序都被隱式授予此權限。」 – mikepenz

+0

@IJJER並且文件未被創建。 (你剛纔提到日誌顯示它不在那裏,但也許它是創建的)。作爲額外的事情,你可以嘗試創建目錄並創建一個新文件來檢查它是否在那裏。 – mikepenz