Android-Create和Access文件保存到可移动SD卡

时间:2016-12-05 20:53:50

标签: android android-sdcard android-file

我想测试可移动SD卡。我可以使用以下代码访问可移动SD卡并获取可用内存:

public static long getTotalExternalMemorySize() {
    String secStore = System.getenv("SECONDARY_STORAGE");
    File path = new File(secStore);
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSizeLong();
    long availableBlocks = stat.getBlockCountLong();
    return availableBlocks * blockSize;
}

我现在想将文件保存到可移动SD卡,但我似乎无法正确使用它。到目前为止,我可以保存在某个地方,但我不确定它在哪里储蓄。我似乎无法弄清楚如何访问创建的文件,然后测试以查看创建的文件是否包含测试短语:" TEST_DATA"。

我真的需要帮助找出保存到可移动SD卡的路径以及保存后如何访问该文件。请帮助,谢谢!!

写入然后读取外部SD卡

    private void createFileonSDcard() {
        File root = getExternalStorageDirectory();
        File dir = new File(root + "/mnt/sdcard/ext_sd/Android/data/com.example.raptorroboticstest/files/mounted/");
        Log.i(log_tag, "the file_path is " + dir.toString());
        //Gave me the SD Card path
//        String appDirectory = (System.getenv("SECONDARY_STORAGE") == null)
//                ? Environment.getExternalStorageDirectory().getAbsolutePath()
//                : System.getenv("SECONDARY_STORAGE");
        Log.e(log_tag, root.toString());
        boolean dirCheck = true;
        if (!dir.exists()) {
            dirCheck = dir.mkdirs();
        }
        if (!dirCheck) {
            Log.e(log_tag, "Unable to make root directory and shutting down App");
            exit_function();
        } else {

            File TestFile = new File(dir, "TestFile.txt");

            Log.d(log_tag, "File exists: " + TestFile.exists());

            //Write the Test File
            try {
                FileOutputStream f = new FileOutputStream(TestFile);
                PrintWriter pw = new PrintWriter(f);
                pw.println("TEST_DATA");
                pw.flush();
                pw.close();
                f.close();
                Log.d(log_tag, "Success writing to the file.");
                final_data = final_data.replace("SD_WRITE|ERROR|FAIL", "SD_WRITE|SUCCESS|PASS");
            } catch (FileNotFoundException e) {
                Log.e(log_tag, "File not found Exception.");
                e.printStackTrace();

            } catch (IOException e) {
                Log.e(log_tag, "I/O Exception.");
                e.printStackTrace();
            }


            //Get the text file
            File file = new File(dir, "TestFile.txt");

            //Read text from file
            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');
                }
            } catch (IOException e) {
                Log.e(log_tag, "Error reading the file");
                e.getStackTrace();
            }
            if (text.toString().contains("SAMPLE_TEXT")) {
                Log.d(log_tag, "Read success");
                final_data = final_data.replace("SD_READ|ERROR|FAIL", "SD_READ|SUCCESS|PASS");
            } else {
                Log.e(log_tag, "Read failed");
            }
            exit_function();
        }
    }

0 个答案:

没有答案