【问题标题】:Write file to sdcard in android在android中将文件写入sdcard
【发布时间】:2013-12-20 14:38:16
【问题描述】:

我想在 sdcard 上创建一个文件。在这里我可以创建文件并将其读/写到应用程序,但我想要的是,文件应该保存在 sdcard 的特定文件夹中。如何使用FileOutputStream 做到这一点?

// create file
    public void createfile(String name) 
    {
        try
        {
            new FileOutputStream(filename, true).close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // write to file

    public void appendToFile(String dataAppend, String nameOfFile) throws IOException 
    {
        fosAppend = openFileOutput(nameOfFile, Context.MODE_APPEND);
        fosAppend.write(dataAppend.getBytes());
        fosAppend.write(System.getProperty("line.separator").getBytes());
        fosAppend.flush();
        fosAppend.close();
    }

【问题讨论】:

  • 查看文档。 developer.android.com/guide/topics/data/data-storage.html。您是否要存储在内部存储中?
  • 是的,我想在内部存储中存储文件...
  • 那么你的问题标题需要更改 将文件写入android的内部存储
  • 对不起,mybad...我想存储在sdcard上...

标签: android file android-sdcard


【解决方案1】:

这是我的代码中的一个示例:

try {
    String filename = "abc.txt";
    File myFile = new File(Environment
            .getExternalStorageDirectory(), filename);
    if (!myFile.exists())
        myFile.createNewFile();
    FileOutputStream fos;
    byte[] data = string.getBytes();
    try {
        fos = new FileOutputStream(myFile);
        fos.write(data);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

别忘了:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

【讨论】:

    【解决方案2】:

    这样试试,

    try {
        File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        try {
            File file = new File(newFolder, "MyTest" + ".txt");
            file.createNewFile();
        } catch (Exception ex) {
            System.out.println("ex: " + ex);
        }
    } catch (Exception e) {
        System.out.println("e: " + e);
    }
    

    【讨论】:

      【解决方案3】:

      在android中创建文件夹和在sd卡上写入文件非常容易

      代码 sn-p

          String ext_storage_state = Environment.getExternalStorageState();
                  File mediaStorage = new File(Environment.getExternalStorageDirectory()
                          + "/Folder name");
                  if (ext_storage_state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
                      if (!mediaStorage.exists()) {
                          mediaStorage.mkdirs();
                      } 
                      //write file writing code..
      
      try {
      
                      FileOutputStream fos=new FileOutputStream(file name);
                      try {
                          fos.write(filename.toByteArray());
                          fos.close();
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }           
                  } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  }
                  else
                  {
                      //Toast message sd card not found..
                  }
      

      【讨论】:

        【解决方案4】:

        注意:'getExternalStorageDirectory()' 实际上为您提供了 /storage/emulated/0 的路径,而不是实际的 sdcard

        '字符串路径 = Syst.envr("SECONDARY_STORAGE");'为您提供通往 SD 卡的路径

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多