繁体   English   中英

android将日志语句写入sdcard

[英]android writing log statements to sdcard

我正在尝试将日志语句写入SD卡。 我决定这样做的方法是通过Application Object在SD卡上创建一个文件。 这样我就可以从应用程序的任何地方调用静态方法logToSdcard()。

创建了包含文件夹“/ RR3log /”,但我记录的每个语句都在其自己的文件中,名为“rr3LogFile.txt”。 所以我有多个rr3LogFile文件,每个文件包含一个staement。

如何将所有语句写入一个rr3LogFile文件? 在此先感谢马特。

public class NfcScannerApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();




        File storageDir = new File(Environment
                .getExternalStorageDirectory(), "/RR3log/");


        storageDir.mkdir();
        try {

            if(outfile == null){
            outfile=File.createTempFile("rr3LogFile", ".txt",storageDir);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }





public static void logToSdcard(String tag, String statement){


        Log.e(TAG, "inside logtosdcard$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");


                String state = android.os.Environment.getExternalStorageState();
                if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
                    try {
                        throw new IOException("SD Card is not mounted.  It is " + state + ".");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                DateTime now = new DateTime();
                DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y H:mm");
                String dateStr = fmt.print(now);


                try{


                    FileOutputStream fOut = new FileOutputStream(outfile);
                    OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
                    myOutWriter.append(dateStr + " " + tag + "     ");
                    myOutWriter.append(statement);
                    myOutWriter.append("\n");
                    myOutWriter.flush();
                    myOutWriter.close();
                    fOut.close();

                }catch(IOException e){
                    e.printStackTrace();
                }




    }


}

然后在应用程序中的任何位置的Activity中。

@Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "inside entryactivity onResume");
        NfcScannerApplication.logToSdcard(TAG, "inside entryactivity onResume" );

logToSdcard方法中,使用附加参数创建FileOutputStream:

FileOutputStream fOut = new FileOutputStream(outfile, true);

true参数表示内容将附加到文件中。 另请参见FileOutputStream

试试看:

public static void printLog(Context context){
    String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
    String command = "logcat -f "+ filename + " -v time -d *:V";

    Log.d(TAG, "command: " + command);

    try{
        Runtime.getRuntime().exec(command);
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

日志将连续保存,直到退出应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM