Android Loger Log class (get built-in SD card)

Source: Internet
Author: User
Tags dateformat readfile

Android phone with internal storage path gets the original address: http://my.oschina.net/liucundong/blog/288183

Direct Sticker Code:

  Public StaticString Getexternalsdcardpath () {if(Sdcardutils.issdcardenable ()) {File Sdcardfile=NewFile (Environment.getexternalstoragedirectory (). GetAbsolutePath ()); returnSdcardfile.getabsolutepath (); } String Path=NULL; File Sdcardfile=NULL; ArrayList<String> devmountlist = getdevmountlist ();  for(String devmount:devmountlist) {File file=NewFile (Devmount); if(File.isdirectory () &&File.canwrite ()) {Path=File.getabsolutepath (); String TimeStamp=NewSimpleDateFormat ("Ddmmyyyy_hhmmss"). Format (NewDate ()); File testwritable=NewFile (Path, "Test_" +TimeStamp); if(Testwritable.mkdirs ()) {testwritable.delete (); } Else{Path=NULL; }            }        }        if(Path! =NULL) {Sdcardfile=NewFile (path); returnSdcardfile.getabsolutepath (); }        return NULL; }
//Read the file     Public Staticstring readFile(string fileName) {Try{File File=NewFile (fileName); FileInputStream FIS=Newfileinputstream (file); intLength =fis.available (); byte[] buffer =New byte[length];            Fis.read (buffer); String Res= encodingutils.getstring (buffer, "UTF-8");            Fis.close (); returnRes; }        Catch(Exception ex) {ex.printstacktrace (); return  NULL; }    }    Private StaticArraylist<string>getdevmountlist () {string[] Tosearch=ReadFile("/etc/vold.fstab"). Split (""); ArrayList<String> out =NewArraylist<string>();  for(inti = 0; i < tosearch.length; i++) {            if(Tosearch[i].contains ("Dev_mount")) {                if(NewFile (Tosearch[i + 2]). Exists ()) {Out.add (tosearch[i+ 2]); }            }        }        returnOut ; }

Get the external SD card path, you can write the log file to the external SD card. The log class is very simple, just a way to use it when you debug, and output the results.

ImportJava.io.File;Importjava.io.IOException;ImportJava.io.RandomAccessFile;Importjava.io.UnsupportedEncodingException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importandroid.os.Environment;/*** Log Printing class, provides a switch to decide whether to print the log*/ Public classlogger{Private Final StaticSimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Private Static FinalSimpleDateFormat pathformate =NewSimpleDateFormat ("YyyyMMdd"); Private Static FinalSimpleDateFormat PATHFORMATE1 =NewSimpleDateFormat ("HH"); Private Static Booleanisrecord=true;//whether logs are logged//memory card directory and phone internal directory    Private Static FinalString Externalstorepath = Fileutil.getexternalsdcardpath () + "Nanbeiyoulog" +file.separator+pathformate.format (NewDate ()) +File.separator; Private Static FinalString FileName = Pathformate1.format (NewDate ()) + ". txt";  Public Static voidd (String tag,string msg) {if(IsRecord) {Try{savetosd (tag,msg); }            Catch(Exception ex) {ex.printstacktrace (); }        }    }    Private Static voidsavetosd (String tag,string msg) {Try{File path=NewFile (Externalstorepath); if(!path.exists ())            {Path.mkdirs (); } File mlogfile=NewFile (externalstorepath+FileName); if(!mlogfile.exists ())            {Mlogfile.createnewfile (); } stringbuffer SB=NewStringBuffer (); Sb.append (Dateformat.format (NewDate ())); Sb.append (": "); Sb.append ("DEBUG"); Sb.append (": ");            Sb.append (tag); Sb.append (": ");            Sb.append (msg); Sb.append ("\ n"); Randomaccessfile RAF=NULL; Try{RAF=NewRandomaccessfile (Mlogfile, "RW");                Raf.seek (Mlogfile.length ()); Raf.write (Sb.tostring (). GetBytes ("UTF-8")); }            Catch(unsupportedencodingexception e) {e.printstacktrace (); }            Catch(IOException e) {e.printstacktrace (); }            finally            {                if(RAF! =NULL)                {                    Try{raf.close (); }                    Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }            }        }        Catch(IOException IoE) {ioe.printstacktrace (); }    }}

Android Loger Log class (get built-in SD card)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.