0

There is a problem with access to file via Windows Explorer. My files are saved on non root device. I can see them only using embedded file manager installe on Android Device, but there is problem with looking them with Windows Explorer. I plug-in and plug-out my device, but it doesn't work good. Here is my code:

public class LoggerUtil {

private File sdRoot;
private String dir;
private File workingDirectory;
private File log;
private Context applicationContext = MApp.getContext();

public LoggerUtil() {
}

@SuppressLint("SimpleDateFormat")
public File createNewLogFile() {
    String currentDateandTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String path = sdRoot.toString() + dir + "log" + currentDateandTime + ".txt";
    return new File(path);
}

@SuppressLint("SimpleDateFormat")
public void appendLog(String text) {

    sdRoot = Environment.getExternalStorageDirectory();
    dir = "/Android/data/" + applicationContext.getPackageName() + "/log/";

    workingDirectory = new File(sdRoot, dir);
    workingDirectory.mkdirs();
    log = createNewLogFile();

    if (ABC.isOK()) {
        File logFile = log;
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            String currentDateandTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            // BufferedWriter for performance, true to set append to file
            // flag
            BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
            buf.append(currentDateandTime + " " + text);
            buf.newLine();
            buf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

My execution is:

 LoggerUtil loger = new LoggerUtil();
   loger.appendLog("TEST");
waclaw
  • 433
  • 1
  • 7
  • 18

0 Answers0