file.createNewFile() 显示异常?
file.createNewFile() show the exception?
在我的项目中,我需要收集崩溃 messages.The 以下是我的演示,代码转到 file.createNewFile()
它会抛出异常,说不是这样的文件或目录,请帮助我.我不知道这是为什么。
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
throw new RuntimeException("Myself throw a exception……");
}
});
}
}
public class MyApplication extends Application {
private static MyApplication sMyApplication;
@Override
public void onCreate() {
super.onCreate();
// 在这里为应用设置异常处理,然后程序才能获取未处理的异常
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this);
}
public static MyApplication getInstance(){
return sMyApplication;
}
}
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "CrashHandler";
private static final boolean DEBUG = true;
private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/CrashTest/log/";
private static final String FILE_NAME = "crash";
private static final String FILE_NAME_SUFFIX = ".trace";
private static CrashHandler sCrashHandler = new CrashHandler();
private Thread.UncaughtExceptionHandler mDefaultCrashHandler;
private Context mContext;
private CrashHandler(){}
// 单例模式
public static CrashHandler getInstance(){
return sCrashHandler;
}
public void init(Context context){
mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mContext = context.getApplicationContext();
}
/**
* 这是最关键的函数,当程序中有未捕获的异常,系统将会自动调用此方法
* @param thread 为出现未捕获异常的线程
* @param exception 未捕获的异常,有了此异常,我们就能得到异常信息
*/
@Override
public void uncaughtException(Thread thread, Throwable exception) {
try {
//save exception message to sdcard
saveExceptionToSDCard(exception);
//upload exception message to web server
uploadExceptionToServer(exception);
} catch (IOException e) {
e.printStackTrace();
}
// 如果系统提供了默认的异常处理器,就交给系统自己处理,否则就自己结束掉自己
if (mDefaultCrashHandler!=null){
mDefaultCrashHandler.uncaughtException(thread,exception);
}else {
Process.killProcess(Process.myPid());
}
}
// 将异常信息保存到SDCard
private void saveExceptionToSDCard(Throwable ex) throws IOException {
// 如果SD卡不存在或无法使用,则无法写入异常信息,给与提示
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
if (DEBUG){
Log.w(TAG,"sdcard unmounted , skip save exception"); //sd卡未安装好,跳出存储异常
}
return;
}
// 文件存储路径
File dir = new File(PATH);
if (!dir.exists()){
dir.mkdir();
}
// 获取当前时间
long current = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current));
// 创建存储异常信息的文件
File file = new File(dir.getAbsolutePath()+"/"+FILE_NAME+time+FILE_NAME_SUFFIX);
if (!file.exists()){
try {
file.createNewFile(); //I debug when code goes to here will throw exception
}catch (Exception e){
e.printStackTrace();
}
}
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
pw.print(time);
savePhoneInfo(pw);
pw.println();
ex.printStackTrace(pw); //输出异常信息
pw.close();
}catch (PackageManager.NameNotFoundException e){
Log.e(TAG,"save crash info failed");
}
}
// 保存手机的信息
private void savePhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),PackageManager.GET_ACTIVITIES);
// APP的版本信息
pw.print("APP Version:");
pw.print(pi.versionName);
pw.print('_');
pw.print(pi.versionCode);
// Android 手机版本号
pw.print("OS Version:");
pw.print(Build.VERSION.RELEASE);
pw.print('_');
pw.print(Build.VERSION.SDK_INT);
// 手机制造商
pw.print("Vendor:");
pw.print(Build.MANUFACTURER);
// 手机型号
pw.print("Model:");
pw.print(Build.MODEL);
// CPU架构
pw.print("CUP ABI:");
pw.print(Build.CPU_ABI);
}
// 将异常信息上传到服务器
private void uploadExceptionToServer(Throwable ex){
}
}
在 saveExceptionToSDCard()
中将 dir.mkdir()
更改为 dir.mkdirs()
在我的项目中,我需要收集崩溃 messages.The 以下是我的演示,代码转到 file.createNewFile()
它会抛出异常,说不是这样的文件或目录,请帮助我.我不知道这是为什么。
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
throw new RuntimeException("Myself throw a exception……");
}
});
}
}
public class MyApplication extends Application {
private static MyApplication sMyApplication;
@Override
public void onCreate() {
super.onCreate();
// 在这里为应用设置异常处理,然后程序才能获取未处理的异常
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this);
}
public static MyApplication getInstance(){
return sMyApplication;
}
}
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static final String TAG = "CrashHandler";
private static final boolean DEBUG = true;
private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/CrashTest/log/";
private static final String FILE_NAME = "crash";
private static final String FILE_NAME_SUFFIX = ".trace";
private static CrashHandler sCrashHandler = new CrashHandler();
private Thread.UncaughtExceptionHandler mDefaultCrashHandler;
private Context mContext;
private CrashHandler(){}
// 单例模式
public static CrashHandler getInstance(){
return sCrashHandler;
}
public void init(Context context){
mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mContext = context.getApplicationContext();
}
/**
* 这是最关键的函数,当程序中有未捕获的异常,系统将会自动调用此方法
* @param thread 为出现未捕获异常的线程
* @param exception 未捕获的异常,有了此异常,我们就能得到异常信息
*/
@Override
public void uncaughtException(Thread thread, Throwable exception) {
try {
//save exception message to sdcard
saveExceptionToSDCard(exception);
//upload exception message to web server
uploadExceptionToServer(exception);
} catch (IOException e) {
e.printStackTrace();
}
// 如果系统提供了默认的异常处理器,就交给系统自己处理,否则就自己结束掉自己
if (mDefaultCrashHandler!=null){
mDefaultCrashHandler.uncaughtException(thread,exception);
}else {
Process.killProcess(Process.myPid());
}
}
// 将异常信息保存到SDCard
private void saveExceptionToSDCard(Throwable ex) throws IOException {
// 如果SD卡不存在或无法使用,则无法写入异常信息,给与提示
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
if (DEBUG){
Log.w(TAG,"sdcard unmounted , skip save exception"); //sd卡未安装好,跳出存储异常
}
return;
}
// 文件存储路径
File dir = new File(PATH);
if (!dir.exists()){
dir.mkdir();
}
// 获取当前时间
long current = System.currentTimeMillis();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(current));
// 创建存储异常信息的文件
File file = new File(dir.getAbsolutePath()+"/"+FILE_NAME+time+FILE_NAME_SUFFIX);
if (!file.exists()){
try {
file.createNewFile(); //I debug when code goes to here will throw exception
}catch (Exception e){
e.printStackTrace();
}
}
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
pw.print(time);
savePhoneInfo(pw);
pw.println();
ex.printStackTrace(pw); //输出异常信息
pw.close();
}catch (PackageManager.NameNotFoundException e){
Log.e(TAG,"save crash info failed");
}
}
// 保存手机的信息
private void savePhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),PackageManager.GET_ACTIVITIES);
// APP的版本信息
pw.print("APP Version:");
pw.print(pi.versionName);
pw.print('_');
pw.print(pi.versionCode);
// Android 手机版本号
pw.print("OS Version:");
pw.print(Build.VERSION.RELEASE);
pw.print('_');
pw.print(Build.VERSION.SDK_INT);
// 手机制造商
pw.print("Vendor:");
pw.print(Build.MANUFACTURER);
// 手机型号
pw.print("Model:");
pw.print(Build.MODEL);
// CPU架构
pw.print("CUP ABI:");
pw.print(Build.CPU_ABI);
}
// 将异常信息上传到服务器
private void uploadExceptionToServer(Throwable ex){
}
}
在 saveExceptionToSDCard()
中将 dir.mkdir()
更改为 dir.mkdirs()