ここに単純な Logger クラス定義を添付したので、そのまま使用できます。ログ情報を SDCARD の Log.txt ファイルに保存するには、at it is を使用します。
package com.clientname.projectname;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.FileHandler;
import android.os.Environment;
import android.util.Log;
/**
* @author Rakesh.Jha
* Date - 07/10/2013
* Definition - Logger file use to keep Log info to external SD with the simple method
*/
public class Logger {
public static FileHandler logger = null;
private static String filename = "ProjectName_Log";
static boolean isExternalStorageAvailable = false;
static boolean isExternalStorageWriteable = false;
static String state = Environment.getExternalStorageState();
public static void addRecordToLog(String message) {
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
isExternalStorageAvailable = isExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
isExternalStorageAvailable = true;
isExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
isExternalStorageAvailable = isExternalStorageWriteable = false;
}
File dir = new File("/sdcard/Files/Project_Name");
if (Environment.MEDIA_MOUNTED.equals(state)) {
if(!dir.exists()) {
Log.d("Dir created ", "Dir created ");
dir.mkdirs();
}
File logFile = new File("/sdcard/Files/Project_Name/"+filename+".txt");
if (!logFile.exists()) {
try {
Log.d("File created ", "File created ");
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.write(message + "\r\n");
//buf.append(message);
buf.newLine();
buf.flush();
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
このファイルを作成したら、ログ情報を log.txt ファイルに保存したい場所で、以下のコードを使用します。-
package com.clientname.projectname;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
/**
* @author Rakesh.Jha
* Date - 03/10/2013
* Definition - //ToDO
*/
public class MainActivity extends Activity {
private static final String TAG = null;
Logger logger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Testing :","log"); // no need to do this line, use below line
logger.addRecordToLog("Testing : log " );
logger.addRecordToLog("TAG MediaPlayer audio session ID: " );
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.test);//test is audio file, u have to keep in raw folder
logger.addRecordToLog( "MediaPlayer audio session ID: " + mediaPlayer.getAudioSessionId());
logger.addRecordToLog( "Media Player started " + "Started !");
mediaPlayer.start(); // no need to call prepare(); create() does that for you
}
private void prepareMediaServer() { }
}