安卓 保存log信息到本地或U盘

2 篇文章 0 订阅
1 篇文章 0 订阅

项目中遇到个情况,在电视主板上开发应用时需要调试信息,但是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;
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值