2013-06-24 212 views
1

對不起,我的英語,但我想寫在這個論壇,因爲在我看來是最好的。 現在我的問題: 我想創建一個文件夾在內部存儲與2應用程序共享。 在我的應用程序中,我從我的服務器上下載了一個Apk並運行它。 在我使用外部存儲並且一切正常之前。 現在我想爲沒有外部存儲的用戶使用內部存儲。在內部存儲中創建一個公共文件夾

我用這個:

String folderPath = getFilesDir() + "Dir" 

但是當我嘗試運行APK,不能正常工作。我無法在手機上找到這個文件夾。

謝謝。

+0

您需要實際創建目錄及其中的文件,並適當設置權限模式。您可以查看File類的文檔。 –

+1

[如何在Android中創建文件夾到SD卡中](http://stackoverflow.com/questions/6911041/how-to-create-folder-into-sd-card-in-android) –

+0

2 Brian Roach ,這個問題是關於外部存儲的,而不是內部的。 –

回答

1

this後:

正確方法:

  1. 您想要的目錄中創建一個文件(例如,文件路徑=新
  2. 文件( getFilesDir(),「myfolder」);)
  3. 如果該文件不存在,則調用該文件上的mkdirs()
  4. 爲輸出文件創建文件(例如,文件mypath = new File(path,「myfile.txt」);)
  5. 使用標準Java I/O寫入該文件(例如,使用新的BufferedWriter新的FileWriter(mypath中)))

享受。

還創建公共文件我用:

/** 
* Context.MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. 
* Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. 
*/ 

public static void createInternalFile(Context theContext, String theFileName, byte[] theData, int theMode) 
{ 
    FileOutputStream fos = null; 

    try { 
     fos = theContext.openFileOutput(theFileName, theMode); 
     fos.write(theData); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e(TAG, "[createInternalFile]" + e.getMessage()); 
    } catch (IOException e) { 
     Log.e(TAG, "[createInternalFile]" + e.getMessage()); 
    } 
} 

只需設置theMode到MODE_WORLD_WRITEABLE或MODE_WORLD_READABLE(注意,它們是從API棄用拉特17)。

您還可以使用theContext.getDir();但要注意什麼醫生說:

檢索,創建如果需要的話,一個新的目錄中,應用程序可以將自己的定製數據文件。您可以使用返回的File對象來創建和訪問此目錄中的文件。請注意,通過File對象創建的文件只能由您自己的應用程序訪問;您只能設置整個目錄的模式,而不是單個文件。

祝好。

+0

我鋼鐵沒有看到我的文件夾。 – Anto

+0

我可以寫入下載文件夾,媒體文件夾或其他文件夾到內部存儲器嗎?謝謝 – Anto

-1

嘗試以下

File mydir = context.getDir("Newfolder", Context.MODE_PRIVATE); //Creating an internal dir; 
if(!mydir.exists) 
{ 
    mydir.mkdirs(); 
}  
+0

Context.MODE_PRIVATE - 將使該文件夾privite))不要設置它,如果你想公開.. –

+0

我試圖不設置它,但我看不到數據夾中的數據/ data/My_Package/Folder – Anto

-1

這是我用什麼,是我工作的罰款:

字符串extStorageDirectory = Environment.getExternalStorageDirectory()。的toString();

File file = new File(extStorageDirectory,fileName);

File parent=file.getParentFile(); 
     if(!parent.exists()){ 
      parent.mkdirs(); 
     } 

這將創建一個新的目錄,如果尚不存在或使用現有的,如果已經存在。

+0

請修復對外部存儲的引用,因爲這裏的目標是使用內部存儲。 –

+0

我需要在內部存儲器中寫入 – Anto

1

您可以創建一個公共到現有系統中的公共文件夾,裏面是從內部存儲一些公共文件夾訪問:

public static String DIRECTORY_MUSIC = "Music"; 
public static String DIRECTORY_PODCASTS = "Podcasts"; 
public static String DIRECTORY_RINGTONES = "Ringtones"; 
public static String DIRECTORY_ALARMS = "Alarms"; 
public static String DIRECTORY_NOTIFICATIONS = "Notifications"; 
public static String DIRECTORY_PICTURES = "Pictures"; 
public static String DIRECTORY_MOVIES = "Movies"; 
public static String DIRECTORY_DOWNLOADS = "Download"; 
public static String DIRECTORY_DCIM = "DCIM"; 
public static String DIRECTORY_DOCUMENTS = "Documents"; 

要創建文件夾,使用此代碼:

File myDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "MyPublicFolder"); 
myDirectory.mkdir(); 

隨着這個例子中,公共將在文檔中創建,並且可以在Android的任何文件的資源管理器應用程序中看到。

相關問題