3

我会将我的文件保存到下载文件夹中。在我的应用程序中,我调用此方法:

public void export(List<VarA> a) {
    String filename = "text.txt";
    FileOutputStream outputStream;      
    try {
        outputStream = openFileOutput(filename, Context.MODE_APPEND);
        List<String> data = new ArrayList<String>();
        data.add("This is my file.\n\n");
        for (int i=0; i<esami.size(); i++) {
            data.add("\n\nName: "+ a.get(i).getName()+"\n");
        }
        outputStream.write(data.toString().getBytes());
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

有了这个,我将文件保存到 data/data/app/files/,我必须更改什么才能保存到 mnt/sdcard/download?谢谢!

4

1 回答 1

1
String externalStoragePath = Environment.getExternalStorageDirectory().toString();
File file = new File(externalStoragePath + "/download/text.txt");
FileOutputStream outputStream;  
try {
    outputStream = new FileOutputStream(file);
    List<String> data = new ArrayList<String>();
    data.add("This is my file.\n\n");
    for (int i=0; i<esami.size(); i++) {
        data.add("\n\nName: "+ a.get(i).getName()+"\n");
    }
    outputStream.write(data.toString().getBytes());
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(outputStream!=null) outputStream.close();
}

你还需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

于 2013-07-18T22:42:01.783 回答