2017-01-20 3 views
0

내 프로젝트에서 충돌 메시지를 수집해야합니다. 다음은 내 데모 코드입니다. 코드는 file.createNewFile()으로 넘어 가고 예외를 throw합니다. 그런 파일이나 디렉토리는 그렇지 않습니다. 제발 도와주세요. 나는 이것이 왜 있는지 모른다.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){ 

    } 
} 

답변

1

dir.mkdir() 감사

+0

dir.mkdirs()로 변경 saveExceptionToSDCard()에, 나는 낮은 실수, 그것은 작동하는지 확인 –