【问题标题】:Create a TXT File in SD Card and write into it in android not working在 SD 卡中创建一个 TXT 文件并在 android 中写入它不起作用
【发布时间】:2016-08-15 08:51:17
【问题描述】:
String fileName = "sample";
try {
     final File f= new File(Environment.getExternalStorageDirectory(), fileName);
     FileOutputStream fo = new FileOutputStream(f);
     fo.write(code.getBytes());
     fo.close();
     Toast.makeText(getBaseContext(),"Done writing SD 'mysdfile.txt'",
               Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
         Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
         Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }

我需要将名为 code 的字符串复制到名为 sample 的文件中。

如果我这样做,我不会收到任何错误。但是,如果使用我的手机并调试到我的 SD 卡,则不会创建文件。谁能告诉我我做错了什么??

【问题讨论】:

  • if use my connected android phone as emulator 。 ???如何做到这一点?
  • and look into my sdcard , no file is created。确实。它不是在 micro SD 卡上创建的,而是在手机的外部存储器上创建的。
  • I dont get any errors. 你应该干杯!哪一个?
  • String fileName = "sample";"Done writing SD 'mysdfile.txt'"。您可以通过不对其进行硬编码而是使用file.getName()file.getAbsolutePath() 来防止此类令人困惑的消息。有了最后一个,您就知道必须准确查看的位置。
  • 错了。在您的设备上使用文件资源管理器应用。

标签: android file android-studio android-sdcard


【解决方案1】:

你可以改变文件对象

File file=new File(Environment.getExternalStorageDirectory().toString()+ Environment.DIRECTORY_DOCUMENTS+"/myfile.txt");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多