简体   繁体   English

使用项目探戈开发者平板电脑在外部micro-SD卡上写入和读取文本文件

[英]write & read textfiles on external micro-sd card with project tango developer tablet

I want to read and write textfiles on the external micro-sd card from my project tango developer tablet. 我想从我的项目探戈开发者平板电脑上读取和写入外部micro-SD卡上的文本文件。

This is my code for reading (it works): 这是我的阅读代码(它有效):

            /* Checks if external storage is available for read and write */
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                String secondaryStore = System.getenv("SECONDARY_STORAGE") + "/Testordner";
                Log.d("secondaryStore", "" + secondaryStore);
                File file = new File(secondaryStore, "file.txt");
                StringBuilder text = new StringBuilder();
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String line;
                    while ((line = br.readLine()) != null) {
                        text.append(line);
                        text.append('\n');
                    }
                    br.close();
                } catch (Exception e) {
                    Log.d("Exception", "");
                }
                Log.d("Text", "" + text.toString());
            }

And this is my code for writing (it doesn't work yet): 这是我写的代码(它还没有用):

              try {
                    File file2 = new File(secondaryStore,"file2.txt");
                    FileWriter writer = new FileWriter(file2);
                    writer.append("Hello World!");
                    writer.flush();
                    writer.close();
                } catch (Exception e) {
                    Log.d("Exception", "");
                }

What am I doing wrong? 我究竟做错了什么? Or is there still an error with the tango tablet? 或者探戈平板电脑是否仍有错误? I found these two links, but trying to fix the problem with these two apps don't work: 我找到了这两个链接,但试图解决这两个应用程序的问题不起作用:

https://github.com/chucknology/TangoSDfix https://github.com/chucknology/TangoSDfix

https://github.com/chucknology/TangoRoot https://github.com/chucknology/TangoRoot

I had the same problem, when creating new files. 创建新文件时遇到了同样的问题。 The file is actually created but android filesystem is not updated with the new file (you can actually see it if you restart your tablet). 该文件实际上已创建,但android文件系统未使用新文件更新(如果重新启动平板电脑,您实际上可以看到它)。 Adding this line after closing my file actually helped android filesystem to notice my file right away. 关闭我的文件后添加此行实际上帮助android文件系统立即注意到我的文件。

MediaScannerConnection.scanFile(mActivity, new String[]{file.toString()}, null, null);

Hope that it works for you. 希望它对你有用。

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

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