简体   繁体   English

Android应用程序写入sdcard文件

[英]Android application write to sdcard files

I have written the following code to write to a file on external storage: 我编写了以下代码以写入外部存储中的文件:

    public static BufferedWriter out;
    private void createFileOnDevice(Boolean append) throws IOException {
            File Root = Environment.getExternalStorageDirectory();
            Log.d("File",Root.toString());
            Root.setWritable(true);
            Root.setReadable(true);
            Log.d("File","Writable: " + Root.canWrite());
            if(Root.canWrite()){
                 File  LogFile = new File(Root, "/Android/DriverInterfaceSystem");
                 if(!LogFile.mkdirs()){
                     Log.d("File","Couldn't make directories");
                 }
                 LogFile = new File(LogFile, "/trace.txt");

                 Log.d("File",LogFile.toString());
                 try{
                     FileWriter LogWriter = new FileWriter(LogFile, true);
                     LogWriter.write("--- New Record ---");
                     out = new BufferedWriter(LogWriter);
                 } catch(IOException e){
                     Log.d("File","FileWriter failed");
                 }

                 out.write("New record");

            }
    }

I have set permissions in Manifest to WRITE_EXTERNAL_STORAGE . 我已将清单中的权限设置为WRITE_EXTERNAL_STORAGE

I know that having both a FileWriter and BufferedWriter is redundant, but I was testing if it was just one of them that wouldn't work. 我知道同时拥有FileWriter和BufferedWriter是多余的,但是我正在测试它是否只是其中之一而无法使用。

When I run, the file is created in the correct location, but the program refuses to write either "-- New Record --" or "New Record" to the file, the file is just empty. 当我运行时,在正确的位置创建了文件,但是程序拒绝将“-New Record-”或“ New Record”写入文件,文件只是空的。

Given that the file is created, it seems that I'm missing one really simple step, but I'm damned if I can see it. 鉴于已经创建了文件,似乎我错过了一个非常简单的步骤,但是如果能看到它,那该死的。 Any thoughts? 有什么想法吗?

You forgot to call flush() and close() on your FileWriter . 您忘记在FileWriter上调用flush()close()了。


Edit #1: 编辑#1:

I'd also like to point out that the following code is unnecessary: 我还要指出,以下代码是不必要的:

root.setWritable(true);
root.setReadable(true);

The call to getExternalStorageDirectory() returns a readable/writable File by default (assuming you've given your application the proper permissions). 默认情况下,对getExternalStorageDirectory()的调用将返回一个可读/可写的File (假设您已为应用程序授予适当的权限)。


Edit #2: 编辑#2:

Last thing. 最后一件事。 Please make sure that before you do any work with the external storage, you always call getExternalStorageState() to check whether the media is available. 请确保在使用外部存储进行任何操作之前,始终调用getExternalStorageState()来检查介质是否可用。 The media might be mounted to a computer, missing, read-only, or in some other state. 介质可能已安装到计算机上,丢失,只读或处于其他某种状态。 See the documentation for more information. 请参阅文档以获取更多信息。

I think you have to call flush() for it to actually write to the file. 我认为您必须调用flush()才能将其实际写入文件。 I think just calling write(...) only adds it to the buffer to be written. 我认为仅调用write(...)只会将其添加到要写入的缓冲区中。 Here a quick example of usage: 这里是用法的简单示例:

FileWriter fWriter;
try{
    fWriter = new FileWriter(“\sdcard\filename.txt”);
    fWriter.write(yourString);
    fWriter.flush();
    fWriter.close();
}catch(Exception e){
    e.printStackTrace();
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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