如何在 android 中为 FileOutputStream 动态设置文件名?
How to set filename dynamically for FileOutputStream in android?
我的要求是文件格式为 filename.extension(.png,.mp4.mp3,txt,.....etc)。
我想知道我该怎么做。
示例代码行:
File extStore = Environment.getExternalStorageDirectory();
FileOutputStream fos = new FileOutputStream(extStore + "/Get_file/filename.jpg");
鉴于我的上述方法?
Environment.getExternalStorageDirectory()
returns 实际外部存储目录作为 java
File
对象调用 getAbsolutePath()
returns 其绝对路径然后简单连接所需的文件名或路径并创建一个 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);
在文件名后添加时间戳,这样每次文件名都不一样。
问题的答案:
File namefile= new File(newFile);
en_Name=namefile.getName();
*** //pass the file name to fileoutputstream//***
FileOutputStream fos = new FileOutputStream(extStore + "/folder/"+en_name);
终于明白了...
我的要求是文件格式为 filename.extension(.png,.mp4.mp3,txt,.....etc)。
我想知道我该怎么做。
示例代码行:
File extStore = Environment.getExternalStorageDirectory();
FileOutputStream fos = new FileOutputStream(extStore + "/Get_file/filename.jpg");
鉴于我的上述方法?
Environment.getExternalStorageDirectory()
returns 实际外部存储目录作为 java
File
对象调用 getAbsolutePath()
returns 其绝对路径然后简单连接所需的文件名或路径并创建一个 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);
在文件名后添加时间戳,这样每次文件名都不一样。
问题的答案:
File namefile= new File(newFile);
en_Name=namefile.getName();
*** //pass the file name to fileoutputstream//***
FileOutputStream fos = new FileOutputStream(extStore + "/folder/"+en_name);
终于明白了...