简体   繁体   中英

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.

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/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). Adding this line after closing my file actually helped android filesystem to notice my file right away.

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

Hope that it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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