如何使用代号将文件写入安卓外部公共根文件夹



例如,我想将一个带有文本"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());

如何获取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

你可以得到如下外部存储路径:

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

在这里,我们在外部存储中创建了一个测试文件夹现在您可以创建输出流,如下所示:

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

最新更新