Write file to sdcard in android

后端 未结 4 913
半阙折子戏
半阙折子戏 2020-12-21 02:05

I want to create a file on sdcard. Here I can create file and read/write it to the application, but what I want here is, the file should be saved on specific folder of sdcar

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-21 02:37

    Here's an example from my code:

    try {
        String filename = "abc.txt";
        File myFile = new File(Environment
                .getExternalStorageDirectory(), filename);
        if (!myFile.exists())
            myFile.createNewFile();
        FileOutputStream fos;
        byte[] data = string.getBytes();
        try {
            fos = new FileOutputStream(myFile);
            fos.write(data);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    

    And don't forget the:

    
    
    

提交回复
热议问题