简体   繁体   English

如何使用CodenameOne将文件写入android外部公共根文件夹?

[英]How to write file to android external public root folder using CodenameOne?

For example, I want to write a text file with text "abc" into android device, but I only found 例如,我想将带有文本“ abc”的文本文件写入android设备,但我仅发现

FileSystemStorage.getInstance().getCachesDir() 

:

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

How can I get the path of android external public root folder (eg:which contains Pictures,Music,...)? 如何获取android外部公共根文件夹的路径(例如,其中包含图片,音乐等)?

    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

you can get externalstorage path like below : 您可以获取如下所示的外部存储路径:

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

here we created a test folder in external storage and now you can create your outputStream as below: 在这里,我们在外部存储中创建了一个测试文件夹,现在您可以创建如下的outputStream:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM