简体   繁体   English

将文件写入nexus 4设备不起作用

[英]Writing a file to nexus 4 device is not working

I am trying to create a file on device using the following code 我正在尝试使用以下代码在设备上创建文件

private void createFileOnDevice(Boolean append) throws IOException {

        File Root = Environment.getExternalStorageDirectory();
        if (Root.canWrite()) {
            File LogFile = new File(Root, "log.txt");
            FileWriter LogWriter = new FileWriter(LogFile, append);
            out = new BufferedWriter(LogWriter);
            Date date = new Date();
            out.write("Logged at"
                    + String.valueOf(date.getDay() + "-" + date.getHours()
                            + ":" + date.getMinutes() + ":" + date.getSeconds()
                            + "\n"));
        }
    }

The code works fine, when i run on devices which has android version 2.3.5 to 4.3 it creates a file. 代码运行良好,当我在具有Android 2.3.5至4.3版本的设备上运行时,它将创建一个文件。

But when i run the same app on Nexus 4 , which has latest android version(4.4.2, kitkat), i am unable to create the file. 但是,当我在具有最新Android版本(4.4.2,kitkat)的Nexus 4上运行相同的应用程序时,我无法创建该文件。 Also, I am not getting any error. 另外,我没有任何错误。

How do i fix it??.. 我如何解决它??..

Thanks in advance :):) 提前致谢 :):)

The issue could be the lack of external storage on a nexus 4... 问题可能是在nexus 4上缺少外部存储...

When you are testing, are you testing it in debug mode? 测试时,是否要在调试模式下进行测试? If so your operating system will have access to the storage on the nexus 4 and not the phone. 如果是这样,您的操作系统将可以访问nexus 4上的存储,而不能访问电话。 Try testing it when the phone has access to the storage and see what happens then... 当手机可以访问存储设备时尝试进行测试,然后看看会发生什么...

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

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