简体   繁体   中英

Saving files from app Android

I would save my file into download folder. In my app i call this method:

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();
    }
}

With this i save my file into data/data/app/files/, what i must change to save into mnt/sdcard/download?? Thank you!

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();
}

You also need <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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