今天在学习UncaughtExceptionHandler的时候,准备写一个异常的日子,结果发现控制台有java.io.IOException: open failed: EINVAL (Invalid argument)输出,
而且原本要创建的文件也没有创建成功,看代码找了半天没有发现原因,结果问度娘发现我的文件名中包含了:,发现是在android中,创建文件时,我是用的是如下代码
private void writeToSDcard(Throwable ex) throws IOException, PackageManager.NameNotFoundException {
//如果没有SD卡,直接返回
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
File filedir = new File(PATH);
if (!filedir.exists()) {
filedir.mkdirs();
}
long currenttime = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date(currenttime));
File exfile = new File(PATH +File.separator+FILE_NAME+time + FILE_NAME_SUFEIX);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(exfile)));
Log.e("错误日志文件路径",""+exfile.getAbsolutePath());
pw.println(time);
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
//当前版本号
pw.println("App Version:" + pi.versionName + "_" + pi.versionCode);
//当前系统
pw.println("OS version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT);
//制造商
pw.println("Vendor:" + Build.MANUFACTURER);
//手机型号
pw.println("Model:" + Build.MODEL);
//CPU架构
pw.println("CPU ABI:" + Build.CPU_ABI);
ex.printStackTrace(pw);
pw.close();
}