项目中遇到个情况,在电视主板上开发应用时需要调试信息,但是adb和串口的使用均受到限制,无法捕获调试信息,想到使用IO流的方式将log信息保存到本地,特定条件下可复制到U盘中
首先是定义保存log信息的文件名和保存路径
File appFile = mContext.getFilesDir();
String path = appFile.getAbsolutePath(); //本地存储路径
String fileName = "adb_log.txt"; //文件名
String srcFilePath = path+"/"+fileName; //文件保存完整路径
使用路径生成File,并得到bufferwriter
FileOutputStream outputStream = new FileOutputStream(srcFile, false);
OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(streamWriter);
向文件中写入log信息
writer.append(data+"\n"); //data为要写入到文件的log信息
writer.flush(); //写入
复制文件到U盘
//复制到U盘
Runtime.getRuntime().exec("cp "+srcFile+" "+desPath); //srcFile是源文件路径
Runtime.getRuntime().exec("ls -l "+desPath); //desPath是目标路径
完整代码
private static BufferedWriter writer = null;
public static void initSaveLog(Context context){
mContext = context;
}
public static void saveAdbLog(String log,boolean closeWriter) {
File appFile = mContext.getFilesDir();
String path = appFile.getAbsolutePath();
String fileName = "adb_log.txt";
String srcFilePath = path+"/"+fileName;
WriteStringToFile(mContext,srcFilePath,log,closeWriter);
}
//读取log信息保存到本地文件
@SuppressWarnings("resource")
private static void WriteStringToFile(Context mContext,String srcFileilePath, String data,boolean closeWriter) {
File srcFile = new File(srcFileilePath);
//创建txt文本准备写入log
if (!srcFile.exists()) {
try {
srcFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//写入log
try {
if (srcFile.exists()) {
if (writer == null) {
FileOutputStream outputStream = new FileOutputStream(srcFile, false);
OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(streamWriter);
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(srcFile, false),"UTF-8"));
}else {
writer.append(data+"\n");
writer.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (writer!=null && closeWriter) {
//关流
writer.close();
writer = null;
//复制log文件到U盘
String[] desPaths = getExternelUsbPaths(mContext);
if (desPaths!=null) {
String desPath = desPaths[0]+"/log/"+"/";
//创建目录
File desDirFile = new File(desPath);
if (!desDirFile.exists()) {
desDirFile.mkdir();
}
//复制到U盘
Runtime.getRuntime().exec("cp "+srcFile+" "+desPath);
Runtime.getRuntime().exec("ls -l "+desPath);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String[] getExternelUsbPaths(Context cn) {
String[] paths = null;
StorageManager storageManager = (StorageManager)cn.getSystemService(Context.STORAGE_SERVICE);
try {
paths = (String[]) StorageManager.class.getMethod("getVolumePaths",
null).invoke(storageManager, null);
} catch (Exception e){
e.printStackTrace();
}
if (paths == null)
return paths;
if (paths.length > 1) {
ArrayList<String> array = new ArrayList<String>();
String[] temp = new String[paths.length];
for (int i = 0; i < paths.length; i++) {
File f = new File(paths[i]);
if (paths[i].equals(getLocalPath()) == false && f.exists()) {
if(f.getFreeSpace()>100)//Double Check the usb Storage is still exit
{
array.add(paths[i]);
}
}
}
temp = new String[array.size()];
array.toArray(temp);
return temp;
} else{
return null;
}
}
private static String getLocalPath() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return Environment.getExternalStorageDirectory().toString();
}
return null;
}