Creating and Storing Log File on device in Android Creating and Storing Log File on device in Android android android

Creating and Storing Log File on device in Android


Wary or not, External Storage still may be the only way to go. Without root access on the device, you can't really get at anything "Internal" unless you're going to be okay with reading within an application on the device. The docs provide pretty solid guidelines for where to create external files, and if you are using API Level 8 or higher, there are a couple of extra functions that can be used. I'm sure you know this page, but here it is anyway: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

If you're in need of any file io example code... I think I could dig some up...

EDIT - I would start by following the guidelines in the above docs to first confirm the state of the storage. I unfortunately don't have any experience with appending a file in Java, so someone else would definitely be more qualified to answer. This doesn't cover appending, but I have a backup routine in one of my personal apps that looks something like this.

    File backupPath = Environment.getExternalStorageDirectory();    backupPath = new File(backupPath.getPath() + "/Android/data/com.maximusdev.bankrecord/files");    if(!backupPath.exists()){        backupPath.mkdirs();    }    FileOutputStream fos;    try {        fos = new FileOutputStream(backupPath.getPath() + "/recordsbackup.txt");        if(okaytowrite){            for(int i = 0; i < count; ++i){                entry = adapter.getItem(i);                fos.write(entry.toString().getBytes());                fos.write("\n".getBytes());                fos.write(String.valueOf(entry.dateTime).getBytes());                fos.write("\n".getBytes());                fos.write(String.valueOf(entry.sign).getBytes());                fos.write("\n".getBytes());                fos.write(String.valueOf(entry.cleared).getBytes());                fos.write("\n".getBytes());                fos.write(String.valueOf(entry.transDate).getBytes());                fos.write("\n".getBytes());                fos.write(entry.category.getBytes());                fos.write("\n".getBytes());            }        }        fos.close();        Toast.makeText(this, "Backup Complete", Toast.LENGTH_SHORT).show();    } catch (FileNotFoundException e) {        e.printStackTrace();        AlertDialog.Builder delmessagebuilder = new AlertDialog.Builder(this);        delmessagebuilder.setCancelable(false);        delmessagebuilder.setMessage("File Access Error");        delmessagebuilder.setNeutralButton("Okay", new DialogInterface.OnClickListener() {            public void onClick(DialogInterface dialog, int id) {                dialog.dismiss();            }        });        delmessagebuilder.create().show();    } catch (IOException e) {        e.printStackTrace();        AlertDialog.Builder delmessagebuilder = new AlertDialog.Builder(this);        delmessagebuilder.setCancelable(false);        delmessagebuilder.setMessage("File Access Error");        delmessagebuilder.setNeutralButton("Okay", new DialogInterface.OnClickListener() {            public void onClick(DialogInterface dialog, int id) {                dialog.dismiss();            }        });        delmessagebuilder.create().show();    }

Once I'm ready to write, I'm pulling a custom object (entry) out of an ArrayAdapter (adapter) and converting field valuse to strings and using getBytes() to pass to the FileOutputStream write function. I've done some research and there are quite a few other options for file writing in Java/Android... the FileWriter Class for instance, so it bears further research.


I used a very simple approach to write String messages to the log file by creating a FileWriter object.

    public static BufferedWriter out;        private void createFileOnDevice(Boolean append) throws IOException {                /*                 * Function to initially create the log file and it also writes the time of creation to file.                 */                File Root = Environment.getExternalStorageDirectory();                if(Root.canWrite()){                     File  LogFile = new File(Root, "Log.txt");                     FileWriter LogWriter = new FileWriter(LogFile, append);                     out = new BufferedWriter(LogWriter);                     Date date = new Date();                     out.write("Logged at" + String.valueOf(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "\n"));                      out.close();            }        }

Now the function to write a new message to the log file.

    public void writeToFile(String message){            try {                out.write(message+"\n");                out.close();            } catch (IOException e) {                e.printStackTrace();            }