标签:
Android手机自带内部存储路径的获取 原文地址:http://my.oschina.net/liucundong/blog/288183
直接贴代码:
public static String getExternalSdCardPath() { if (SDCardUtils.isSDCardEnable()) { File sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); return sdCardFile.getAbsolutePath(); } String path = null; File sdCardFile = null; ArrayList<String> devMountList = getDevMountList(); for (String devMount : devMountList) { File file = new File(devMount); if (file.isDirectory() && file.canWrite()) { path = file.getAbsolutePath(); String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date()); File testWritable = new File(path, "test_" + timeStamp); if (testWritable.mkdirs()) { testWritable.delete(); } else { path = null; } } } if (path != null) { sdCardFile = new File(path); return sdCardFile.getAbsolutePath(); } return null; }
//读文件 public static String readFile(String fileName) { try { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); int length = fis.available(); byte [] buffer = new byte[length]; fis.read(buffer); String res = EncodingUtils.getString(buffer, "UTF-8"); fis.close(); return res; } catch (Exception ex) { ex.printStackTrace(); return null; } } private static ArrayList<String> getDevMountList() { String[] toSearch = readFile("/etc/vold.fstab").split(" "); ArrayList<String> out = new ArrayList<String>(); for (int i = 0; i < toSearch.length; i++) { if (toSearch[i].contains("dev_mount")) { if (new File(toSearch[i + 2]).exists()) { out.add(toSearch[i + 2]); } } } return out; }
取得了外部sd卡的路径,就可以把日志文件写到外部sd卡了。日志类非常简单,就一个方法,自己debug的时候用,把结果输出出来。
import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import android.os.Environment; /** * 日志打印类,提供了一个开关来决定是否打印日志 */ public class Logger { private final static SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat PATHFORMATE =new SimpleDateFormat("yyyyMMdd"); private static final SimpleDateFormat PATHFORMATE1 = new SimpleDateFormat("HH"); private static boolean IsRecord=true;//是否记录日志 //存储卡目录和手机内部目录 private static final String ExternalStorePath = FileUtil.getExternalSdCardPath()+"nanbeiyoulog" +File.separator+PATHFORMATE.format(new Date())+File.separator; private static final String FileName = PATHFORMATE1.format(new Date())+".txt"; public static void d(String tag,String msg) { if(IsRecord) { try { SaveToSD(tag,msg); } catch (Exception ex) { ex.printStackTrace(); } } } private static void SaveToSD(String tag,String msg) { try{ File path = new File(ExternalStorePath); if(!path.exists()) { path.mkdirs(); } File mLogFile = new File(ExternalStorePath+FileName); if(!mLogFile.exists()) { mLogFile.createNewFile(); } StringBuffer sb = new StringBuffer(); sb.append(DATEFORMAT.format(new Date())); sb.append(": "); sb.append("DEBUG"); sb.append(": "); sb.append(tag); sb.append(": "); sb.append(msg); sb.append("\n"); RandomAccessFile raf = null; try { raf = new RandomAccessFile(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 block e.printStackTrace(); } } } } catch (IOException ioe) { ioe.printStackTrace(); } } }
标签:
原文地址:http://www.cnblogs.com/zhouxiuquan/p/4539239.html