public void write(String log) {
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File dirPath = Environment.getExternalStorageDirectory();
File file = new File(dirPath.toString(), "log.txt");
if (file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(log.getBytes("UTF-8"));
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File dirPath = Environment.getExternalStorageDirectory();
File file = new File(dirPath.toString(), "log.txt");
if (file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(log.getBytes("UTF-8"));
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>