在android中将文件写入sdcard

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20369782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:45:08  来源:igfitidea点击:

Write file to sdcard in android

androidfileandroid-sdcard

提问by Looking Forward

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 sdcard. How can I do that using FileOutputStream?

我想在 sdcard 上创建一个文件。在这里,我可以创建文件并将其读/写到应用程序中,但我想要的是,该文件应保存在 sdcard 的特定文件夹中。我怎样才能做到这一点FileOutputStream

// create file
    public void createfile(String name) 
    {
        try
        {
            new FileOutputStream(filename, true).close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // write to file

    public void appendToFile(String dataAppend, String nameOfFile) throws IOException 
    {
        fosAppend = openFileOutput(nameOfFile, Context.MODE_APPEND);
        fosAppend.write(dataAppend.getBytes());
        fosAppend.write(System.getProperty("line.separator").getBytes());
        fosAppend.flush();
        fosAppend.close();
    }

回答by Rakeeb Rajbhandari

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:

并且不要忘记:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

回答by Gunaseelan

Try like this,

试试这样,

try {
    File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
    if (!newFolder.exists()) {
        newFolder.mkdir();
    }
    try {
        File file = new File(newFolder, "MyTest" + ".txt");
        file.createNewFile();
    } catch (Exception ex) {
        System.out.println("ex: " + ex);
    }
} catch (Exception e) {
    System.out.println("e: " + e);
}

回答by Rajith Rajan

It's pretty easy to create folder and write file on sd card in android

在android中创建文件夹并在sd卡上写入文件非常容易

Code snippet

代码片段

    String ext_storage_state = Environment.getExternalStorageState();
            File mediaStorage = new File(Environment.getExternalStorageDirectory()
                    + "/Folder name");
            if (ext_storage_state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
                if (!mediaStorage.exists()) {
                    mediaStorage.mkdirs();
                } 
                //write file writing code..

try {

                FileOutputStream fos=new FileOutputStream(file name);
                try {
                    fos.write(filename.toByteArray());
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            else
            {
                //Toast message sd card not found..
            }

回答by Kat

Note: 'getExternalStorageDirectory()' actually gives you a path to /storage/emulated/0 and not the actual sdcard

注意:“getExternalStorageDirectory()”实际上为您提供了 /storage/emulated/0 的路径,而不是实际的 sdcard

'String path = Syst.envr("SECONDARY_STORAGE");' gives you a path to the sd card

'String path = Syst.envr("SECONDARY_STORAGE");' 为您提供通往 SD 卡的路径