【问题标题】:write & read textfiles on external micro-sd card with project tango developer tablet使用 project tango 开发人员平板电脑在外部 micro-sd 卡上写入和读取文本文件
【发布时间】:2016-04-30 18:05:42
【问题描述】:

我想从我的项目 tango 开发者平板电脑上读取和写入外部 micro-sd 卡上的文本文件。

这是我的阅读代码(有效):

            /* 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());
            }

这是我的写作代码(还不行):

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

我做错了什么?还是探戈平板电脑仍然存在错误?我找到了这两个链接,但尝试解决这两个应用程序的问题不起作用:

https://github.com/chucknology/TangoSDfix

https://github.com/chucknology/TangoRoot

【问题讨论】:

  • 您是否在 AndroidManifest.xml 文件中设置了写入权限。 ?
  • 是的,我检查了用户权限并添加了 WRITE 和 READ_EXTERNAL_STORAGE。我还添加了以下行:file2.createNewFile();但是由于用户权限失败,我总是得到一个 IOException 。我认为这是探戈平板电脑本身的错误。

标签: java android google-project-tango


【解决方案1】:

我在创建新文件时遇到了同样的问题。该文件实际上已创建,但 android 文件系统未使用新文件更新(如果您重新启动平板电脑,您实际上可以看到它)。 在关闭我的文件后添加这一行实际上有助于 android 文件系统立即注意到我的文件。

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

希望它对你有用。

【讨论】:

  • 我很抱歉,但这无济于事。 IOExceptio 在 createNewFile() 函数时被抛出。我们发现使用 project tango 平板电脑无法在外部 sd 卡上写入。
  • 抱歉,我没有看到您收到的是 IOException。我认为可能是您在创建后看不到文件。我没有外接sd卡的解决方案,抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 2018-12-13
  • 1970-01-01
相关资源
最近更新 更多