Categories: Android

[안드로이드] 쓰레드덤프 파일을 sdcard 로 꺼내는 코드

 쓰레드덤프 파일은 “/data/anr/traces.txt” 로 남게 되는데, data 폴더는 실제 폰에서 루팅을 해야만 접근을 할 수 있습니다. 그래서 폰에서도 다른 도구 없이 소스코드를 사용하여 sdcard 로 옮기기 위해서 간단히 파일 복사 코드를 만들어 보았습니다.

[code java]
package com.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


import android.content.Context;
import android.os.Environment;


public class DumpCheck {


 private File mDumpFile = null;
 private final static String DUMP_FILE_PATH = “/data/anr/traces.txt”;
 
 public void checkDumpFile(Context context, boolean deleteDumpFile) throws IOException {
  if(existDumpFile() == true) {
   if(copyDumpFile(context) == true) {
    if(deleteDumpFile == true) {
     deleteDumpFile();
    }
   }
  }
 }
 
 public boolean existDumpFile() {
  if (mDumpFile == null) {
   mDumpFile = new File(DUMP_FILE_PATH);
  }
  if (mDumpFile == null) {
   return false;
  }
  return mDumpFile.exists();
 }
 
 public boolean deleteDumpFile() {
  if (mDumpFile == null) {
   mDumpFile = new File(DUMP_FILE_PATH);
  }
  if (mDumpFile == null || mDumpFile.exists() == false) {
   return false;
  }
  return mDumpFile.delete();
 }
 
 public boolean copyDumpFile(Context context) throws IOException {
  boolean result = false;
  if (mDumpFile == null) {
   mDumpFile = new File(DUMP_FILE_PATH);
  }
  if (mDumpFile == null || mDumpFile.exists() == false) {
   return false;
  }
 
  FileWriter writer = null;
  InputStream inputStream = null;
  try {
   if(mDumpFile.canRead() == false) {
    return false;
   }


   inputStream = new FileInputStream(mDumpFile);
   String fileString = “”;
   int readNum;
   byte in[] = new byte[512];
   while ((readNum = inputStream.read(in, 0, 512)) != -1) {
    fileString += new String(in, 0, readNum);
   }
   
   writer = new FileWriter(new File(Environment.getExternalStorageDirectory(), “logFile” + new Date().getTime() + “.txt”), true);
   writer.write(fileString);
   result = true;
  } catch(Exception ex) {
   ex.printStackTrace();
  } finally {
   if(writer != null) {
    writer.close();
   }
   if(inputStream != null) {
    inputStream.close();
   }
  }
  return result;
 }
}
[/code]

 코드를 추가로 수정 후에 테스트를 제대로 못해봤는데, 문제가 있다면 남겨주시길 바랍니다.



1113960844.java

dingpong

Share
Published by
dingpong

Recent Posts

LFS error at git checkout in Jenkins

Problem stdout: stderr: Downloading xxxx.a (83 MB) error: git-lfs smudge -- 'xxxx.a' died of signal…

3년 ago

플레이모빌 크리스마스 XXL 6629 산타 해외직구 구매가 배송비

https://www.playmobil.de/playmobil-xxl-weihnachtsmann/6629.html 구매가 : 48.49 유로 (플레이모빌 독일 홈페이지) 결제하는데 안되서 PayPal 로 결제하니 잘 잔행…

3년 ago

코카콜라 제로 355ml 72캔 구매가 (캔당 453원)

롯데온에서 355ml 24캔 롯데카드로 구매시 13,210원 https://www.lotteon.com/p/product/PD36294 APP으로 35,000원 이상 구매시 7,000 포인트 적립 행사…

4년 ago

경동나비엔 온수매트 EQM541-QS (퀸사이즈) 구입가

구입 매장 : 현대 홈쇼핑 (홈쇼핑 방송 중) 모바일 앱 http://www.hyundaihmall.com/front/pda/itemPtc.do?slitmCd=2114353981 최종 결제 가격 :…

4년 ago

PostMessage에 shared_ptr과 같은 스마트 포인터 넣기

PostMessage의 WPARAM 이나 LPARAM 으로 shared_ptr 와 같은 스마트 포인터 객체를 넘기고 싶은 경우가 있습니다.…

4년 ago

RC2255 – named STRINGTABLEs are not allowed

Visual Studio 에서 작업 중 이러한 컴파일 에러가 발생한 케이스가 있었습니다. 이 경우에는 .rc 파일에…

4년 ago