2017-06-14 2 views
0

Zum Beispiel, ich möchte eine Textdatei mit dem Text „abc“ in Android-Gerät schreiben, aber ich fand nurWie schreibe ich Datei in Android externe öffentliche Stammordner mit CodenameOne?

FileSystemStorage.getInstance().getCachesDir() 

:

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

Wie kann ich den Weg des android externen öffentlichen bekommen Stammordner (zB: welcher enthält Bilder, Musik, ...)?

Antwort

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

können Sie externalstorage Pfad wie unten erhalten:

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

wir hier einen Testordner in externen Speicher erstellt und jetzt können Sie Ihre output erstellen wie folgt:

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