SDã«ã¼ãã«ãã°ãåºåããæ¹æ³
GPSã使ç¨ããã¢ããªã±ã¼ã·ã§ã³ãéçºãã¦ãå¤ã«åºã¦å®æ©ã§ãã¹ãããã¨ãã©ããã¦ããã®å ´ã§ãã°ã確èªã§ããªãç¶æ³ãçºçãã¾ãã
ãããªæã«ã¯SDã«ã¼ãã«ãã°ãåºåããããã«ãã¦ããã¨ä¾¿å©ã§ãã
以ä¸ã¯SDã«ã¼ãã«ãã°ãåºåããä¾ã§ãã
package jp.bs.util; import java.io.File; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.Date; import android.os.Environment; import android.util.Log; public class SdLog { private final static String LOGDIR = Environment.getExternalStorageDirectory().getPath() + "/hoge/"; private final static String SDFILE = LOGDIR+"log.txt"; private static boolean enable = true; static public void put(String text) { if (!enable) return; Date now = new Date(); BufferedWriter bw = null; StackTraceElement[] ste = (new Throwable()).getStackTrace(); text = ste[1].getMethodName() + "(" + ste[1].getFileName() + ":" + ste[1].getLineNumber() + ") " + text; try { try { mkdir_p(LOGDIR); } catch (IOException e) { e.printStackTrace(); return; } FileOutputStream file = new FileOutputStream(SDFILE, true); bw = new BufferedWriter(new OutputStreamWriter( file, "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } try { bw.append((now.getYear()+1900)+"/"+(now.getMonth()+1)+"/"+now.getDate() +" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+"\t"+text+"\n"); Log.e("log",(now.getYear()+1900)+"/"+(now.getMonth()+1)+"/"+now.getDate() +" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+"\t"+text); } catch (IOException e) { e.printStackTrace(); } try { bw.close(); } catch (IOException e) { e.printStackTrace(); } bw = null; } public static boolean mkdir_p(File dir) throws IOException { if (!dir.exists()) { if (!dir.mkdirs()) { throw new IOException("File.mkdirs() failed."); } return true; } else if (!dir.isDirectory()) { throw new IOException("Cannot create path. " + dir.toString() + " already exists and is not a directory."); } else { return false; } } public static boolean mkdir_p(String dir) throws IOException { return mkdir_p(new File(dir)); } }
Throwableããã¹ã¿ãã¯ãã¬ã¼ã¹ãåå¾ãã¦ãã¡ã¤ã«åã¨ã¡ã½ããåãè¡çªå·ãåã£ã¦ãã¦ããã®ããã¤ã³ãã§ãã
SdLog.put("hogehoge");
ã¨ãã¦ä½¿ãã¨LogCatã®ç»é¢ã«
10-01 22:59:32.149: ERROR/log(9532): 2009/10/1 22:59:31 onCreate(HogeActivity.java:113) hogehoge
ã®ããã«åºåãããã®ã¨åæã«LOGDIRã«è¨å®ãã¦ãããã£ã¬ã¯ããªã«ãã¡ã¤ã«åºåããã¾ãã
ï¼ãã®å ´åã¯ã/sdcard/hoge/log.txtã
å ¨ã¦ã®ãã°åºåããã®æ¹æ³ã«ãã¦ããã°ãSdLogã®enableãfalseã«ãã¦ããã°ãã°åºåãä¸æ¬ã§æ¢ããäºãåºæ¥ã¾ãã
SDã«ã¼ãå ã®ãã¡ã¤ã«ãé²è¦§ã§ããã¢ããªãå ¥ãã¦ç½®ãã¨ãAndroidã®å®æ©æ¬ä½ã ãã§ãã°ã確èªãããã¨ãã§ããé常ã«ä¾¿å©ã§ãã
æ¯éãæ´»ç¨ãã ããã
ãªããSDK1.6ããSDã«ã¼ãæ¸ãè¾¼ã¿ã®ãã¼ããã·ã§ã³ãå¿
è¦ã«ãªãã¾ãããã1.6ã«ã¢ãããã¼ãããGDDPhoneã§ã¯ãã¼ããã·ã§ã³ã®è¨è¿°ãããªãã¦ãæ¸ãè¾¼ã¿ã§ããããã§ãã
ä»å¾å¤æ´ãããããHT-03Aãä»ã®æ©ç¨®ã§ã¯æåãç°ãªãããããã¾ããã®ã§ã1.6以éã使ãå ´åã¯ä»¥ä¸ã®ãã¼ããã·ã§ã³ããããã§ã¹ããã¡ã¤ã«ã«è¨è¿°ãã¦ãããæ¹ãããã§ãããã
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>