【问题标题】:How to set filename dynamically for FileOutputStream in android?如何在android中为FileOutputStream动态设置文件名?
【发布时间】:2016-01-14 19:34:13
【问题描述】:

我的要求是文件格式为 filename.extension(.png,.mp4.mp3,txt,.....etc)。
我想知道我该怎么做。

示例代码行:

 File extStore = Environment.getExternalStorageDirectory();

 FileOutputStream fos = new FileOutputStream(extStore + "/Get_file/filename.jpg");

鉴于我的上述方法?

【问题讨论】:

  • 我不确定我是否理解您的问题,但这里的这一点 "/Get_file/filename.jpg" 是一个字符串,因此您可以根据用户的一些输入或您用来确定的任何内容动态构造该字符串正确的文件名。

标签: java android file file-io android-sdcard


【解决方案1】:

问题的答案:

File namefile= new File(newFile);
en_Name=namefile.getName();

*** //pass the file name to fileoutputstream//***

FileOutputStream fos = new FileOutputStream(extStore + "/folder/"+en_name);    

终于明白了……

【讨论】:

    【解决方案2】:

    在你的文件名后面加上时间戳,这样每次文件名都不一样。

    【讨论】:

      【解决方案3】:

      Environment.getExternalStorageDirectory() 返回实际的外部存储目录作为java File 对象调用getAbsolutePath() 返回其绝对路径,然后简单地连接所需的文件名或它的路径并创建一个new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Get_file/filename.jpg") 并检查如果文件存在.. 如果不存在,则必须创建一个。

      使用类似的东西。

      String filepath = "/Get_file/filename.jpg";
      File yourFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filepath);
      
      if(!yourFile.exists()) {
          yourFile.createNewFile();
      } 
      FileOutputStream oFile = new FileOutputStream(yourFile, false); 
      

      【讨论】:

      • 嗯.. 希望对您有所帮助
      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2015-03-05
      • 2016-09-16
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      相关资源
      最近更新 更多