Windows Explorer を介したファイルへのアクセスに問題があります。私のファイルはルート以外のデバイスに保存されています。Android デバイスの組み込みファイル マネージャー インストールを使用してのみ表示できますが、Windows エクスプローラーで表示すると問題が発生します。デバイスのプラグインとプラグアウトを行っていますが、うまく機能しません。これが私のコードです:
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();
}
}
}
}
私の実行は:
LoggerUtil loger = new LoggerUtil();
loger.appendLog("TEST");