0

I need to get logcat -v time from a device and save it to a text file in a SD Card. The problem is that my application freezes when I press the button to do it.

I understand that it might happen because logcat -v time keeps getting the log of all actions of the device, nonstop. And I need all this information. But I don't know how to code it correctly. Would anyone could help me, please?

Thanks in advance!

Here is my code:

btn_comeca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
try {
                    Process process = Runtime.getRuntime().exec("logcat -v");
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                    StringBuilder log=new StringBuilder();
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        log.append(line);
                    }

                    try {

                        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                            //Verifica permissão de gravar/ler
                            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                            } else {

                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MODE_ENABLE_WRITE_AHEAD_LOGGING);
                            }
                        }
                        File caminho_arquivo = new File("/sdcard/ARQUIVOFINAL.txt");
                        caminho_arquivo.createNewFile();
                        FileOutputStream fileOutputStream = new FileOutputStream(caminho_arquivo);
                        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                        TextView tv = (TextView)findViewById(R.id.texttext);
                        String texto = "";
                        //tv.setText(log.toString());
                        texto = log.toString();
                        //outputStreamWriter.append(tv.getText());
                        outputStreamWriter.append(texto);
                        outputStreamWriter.close();
                        fileOutputStream.close();
                        Toast.makeText(getBaseContext(), "Text File Saved!", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }
                catch (IOException e) {}
}

  • Not understand purpose of doing such a thing, anyway take a look at this https://stackoverflow.com/a/14628593/1025070 – UdayaLakmal Dec 10 '19 at 12:02
  • Actually a device is having some strange behavior and the manufacturer wants me to get its logcat to see everything that is happening in the device. So I decided to make this app to make it easier to get the logcat. – Grace G. B. Dec 10 '19 at 12:10
  • if so without trying to save in device sdcard you can run from your computer with device attached "adb logcat > logcat.txt" that will redirect all logcat output to text file. – UdayaLakmal Dec 10 '19 at 12:16
  • UdayaLakmal, I know that. Using computer works fine. But I thought about this app just to do not use any computer. This will make all things much more easier. – Grace G. B. Dec 10 '19 at 12:23

2 Answers2

0

The problem may be: your logic works synchronously. You should never execute work like that in main thread, since it can lead to ANR (Android Not Responding).

There is plenty of ways to fix that, though. Since you are not using frameworks for multithreading (e.g. RxJava, you can read about it, if you are interested), you can use AsyncTask. You can place this in your listener.

EDIT: However, take a look at MVVM or MVP pattern and multithreading frameworks later. These things are widely used in production for executing different kinds of asynchronous tasks.

new AsyncTask<Void, Void, Void>(){
    @Override
    protected Void doInBackground(Void... params) {
        // Place your logic here
        return;
    }
}.execute();
Steyrix
  • 2,796
  • 1
  • 9
  • 23
0

Normal invocation of logcat won't every exit by itself.

Therefore your code will be forever stuck in your while loop.

As per https://developer.android.com/studio/command-line/logcat

Use the -d "Dump the log to the screen and exits." commandline option.

This is what I use in a similar method to display the logcat output in to a TextView

Andrew
  • 8,198
  • 2
  • 15
  • 35