- private void saveLogcat(String fileNameString) {
- // Get a handle for the logcat text view.
- TextView logcatTextView = findViewById(R.id.logcat_textview);
-
- try {
- // Get the logcat as a string.
- String logcatString = logcatTextView.getText().toString();
-
- // Create an input stream with the contents of the logcat.
- InputStream logcatInputStream = new ByteArrayInputStream(logcatString.getBytes(StandardCharsets.UTF_8));
-
- // Create a logcat buffered reader.
- BufferedReader logcatBufferedReader = new BufferedReader(new InputStreamReader(logcatInputStream));
-
- // Create a file from the file name string.
- File saveFile = new File(fileNameString);
-
- // Create a file buffered writer.
- BufferedWriter fileBufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveFile)));
-
- // Create a transfer string.
- String transferString;
-
- // Use the transfer string to copy the logcat from the buffered reader to the buffered writer.
- while ((transferString = logcatBufferedReader.readLine()) != null) {
- // Append the line to the buffered writer.
- fileBufferedWriter.append(transferString);
-
- // Append a line break.
- fileBufferedWriter.append("\n");
- }
-
- // Close the buffered reader and writer.
- logcatBufferedReader.close();
- fileBufferedWriter.close();
-
- // Add the file to the list of recent files. This doesn't currently work, but maybe it will someday.
- MediaScannerConnection.scanFile(this, new String[] {fileNameString}, new String[] {"text/plain"}, null);
-
- // Display a snackbar.
- Snackbar.make(logcatTextView, getString(R.string.file_saved_successfully), Snackbar.LENGTH_SHORT).show();
- } catch (Exception exception) {
- // Display a snackbar with the error message.
- Snackbar.make(logcatTextView, getString(R.string.save_failed) + " " + exception.toString(), Snackbar.LENGTH_INDEFINITE).show();
- }
- }
-