2015-10-16 113 views
0

我的要求是文件格式为filename.extension(.png,.mp4.mp3,txt,.....等)。
我想知道我该怎么做。如何在android中为FileOutputStream动态设置文件名?

示例代码行:

File extStore = Environment.getExternalStorageDirectory(); 

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

给我上面的方法?

+0

我不知道我理解你的问题,但是这一点在这里'“/Get_file/filename.jpg”'是一个字符串,所以你可以根据用户的输入或者你用来确定正确的文件名来动态地构造该字符串。 – panonski

回答

0

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

使用类似这样的东西。

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); 
+0

它对我有用... –

+0

@vishnupalanivelM你想说什么请详细说明.. – Minato

+0

是啊!我试试看... –

0

在文件名后加上时间戳,所以每次文件名都不一样。

+0

不需要时间戳..谢谢你的回答... –

0

回答的问题是:

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

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

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

终于我知道了......

相关问题