在使用FileOutputStream時經(jīng)常出現(xiàn)FileNotFoundException問題歉提,在開發(fā)中使用部分機型適配時拋出FileNotFoundException問題页衙,有時會莫名其妙的就好了,但是問題總歸存在函似,經(jīng)過調(diào)研發(fā)現(xiàn)了問題所在:
首先槐脏,明確一點FileOutputStream不會幫你創(chuàng)建不存在的路徑,所以使用FileOutputStream要先創(chuàng)建路徑撇寞,再創(chuàng)建文件顿天。
public static final String TEMP=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+"temp"+ File.separator; //路徑名
String newPath = TEMP+ System.currentTimeMillis() +".jpg"; //文件名
File file =newFile(TEMP);
if(!file.exists()) {
file.mkdirs();//新建目錄
}
File newFile =newFile(newPath);
FileOutputStream fs =newFileOutputStream(newFile);
往往只使用最后一行,有時候部分機型不會出錯重抖,但大多數(shù)時候都會行不通