Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中向SD卡写入文件_Android_File_Android Sdcard - Fatal编程技术网

在android中向SD卡写入文件

在android中向SD卡写入文件,android,file,android-sdcard,Android,File,Android Sdcard,我想在SD卡上创建一个文件。在这里,我可以创建文件并将其读/写到应用程序中,但我想要的是,该文件应保存在SD卡的特定文件夹中。如何使用FileOutputStream // create file public void createfile(String name) { try { new FileOutputStream(filename, true).close(); } catch (FileNot

我想在SD卡上创建一个文件。在这里,我可以创建文件并将其读/写到应用程序中,但我想要的是,该文件应保存在SD卡的特定文件夹中。如何使用
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();
    }

下面是我的代码中的一个示例:

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();
    }
别忘了:

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

像这样试试

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

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

代码片段

    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..
            }

注意:'getExternalStorageDirectory()' 实际上为您提供了/storage/simulated/0的路径,而不是实际的SD卡


'字符串路径=系统环境(“辅助存储”);'为您提供sd卡路径

检查文档。您正在尝试将文件存储在内部存储器中吗?是的,我想将文件存储在内部存储器中…然后您的问题标题需要更改,请在Androidorry,mybad中将文件写入内部存储器。。。我想存储在SD卡上。。。