2017-06-14 51 views
0

例如,我想寫的文字「ABC」到Android設備上的文本文件,但我只找到如何使用CodenameOne將文件寫入Android外部公共根文件夾?

FileSystemStorage.getInstance().getCachesDir() 

String filePath=FileSystemStorage.getInstance().getCachesDir()+FileSystemStorage.getInstance().getFileSystemSeparator()+"text.txt"; 
OutputStream out=FileSystemStorage.getInstance().openOutputStream(filePath); 
out.write("abc".getBytes()); 

我怎樣才能得到Android的外部公衆的路徑根文件夾(例如:其中包含圖片,音樂,...)?

回答

0
In AndroidManifest.xml, one should have 
    <manifest ...> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

     ... 
    </manifest> 
Then in the code 
/* Checks if external storage is available for read and write */ 
public boolean isExternalStorageWritable() { 
    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     return true; 
    } 
    return false; 
} 

/* Checks if external storage is available to at least read */ 
public boolean isExternalStorageReadable() { 
    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state) || 
     Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     return true; 
    } 
    return false; 
} 

public 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()) { 
     Log.e(LOG_TAG, "Directory not created"); 
    } 
    return file; 
} 

So, by this way one can code and make use of external directory. Browse the link for more information 

     https://developer.android.com/training/basics/data-storage/files.html gives useful info about external storage option availability 
0

,你可以得到externalstorage路徑如下圖所示:

String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Test"; 
    File dir = new File(dirPath); 
    if (!dir.exists()) 
     dir.mkdirs(); 

在這裏,我們在外部存儲 創建一個測試文件夾,現在你可以如下創建您的OutputStream:

OutputStream out=FileSystemStorage.getInstance().openOutputStream(dirPath+"text.txt"); 
out.write("abc".getBytes());