0

I am running into a issue when trying to save the logcat to a file . Below is the code:

public void createLogs() {
    if (isExternalStorageWritable()) {
        
        File appDirectory;
        if(Build.VERSION.SDK_INT >= 30) {
             appDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/myTest");
        }else{
             appDirectory = new File(Environment.getExternalStorageDirectory() + “/myTest");
        }
        File logDirectory = new File(appDirectory + "/logs");
        
         File logFile = new File(logDirectory, “mytest.txt");

        if (!appDirectory.exists()) {
            appDirectory.mkdir();
        }

        if (!logDirectory.exists()) {
            logDirectory.mkdir();
        }
       
        try {
            //Process process = Runtime.getRuntime().exec("logcat -c");
            Process process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r 2048 -n 4");
        } catch ( IOException e ) {
            e.printStackTrace();
        }

    }

}

It creates the folder myTest-->logs--->mytest.txt in this folder structure. It is creating .txt file but when manually delete .txt is not creating again. Please suggest.

prat
  • 597
  • 8
  • 17
  • `File logDirectory = new File(appDirectory + "/logs");` Change to: `File logDirectory = new File(appDirectory, "logs");` The other ones too. – blackapps Feb 10 '22 at 20:45
  • `if (!appDirectory.exists()) { if(!appDirectory.mkdir()) return; }` The other ones too. – blackapps Feb 10 '22 at 20:46
  • `but when manually delete .txt is creating again. ` That looks ok to me. Sorry i do not see an issue. – blackapps Feb 10 '22 at 20:49
  • my bad, manually deleting the .txt , .txt is not creating again – prat Feb 11 '22 at 04:01
  • After doing the above changes, myTest itself is not creating , hence no logs – prat Feb 11 '22 at 04:13
  • You probably do this on an Android 11+ device. – blackapps Feb 11 '22 at 06:10
  • yes you are right, in Android 10 everything is working fine . Ony in Android 11 ,.txt file gets created only once. And if deleted manually, not creating .txt again though the folders are present – prat Feb 11 '22 at 06:50
  • `but when manually delete .txt ` What is manually? – blackapps Feb 11 '22 at 07:33
  • Manually means, I am reaching to the folder and explicitly deleting the .txt files from the folder. – prat Feb 13 '22 at 17:01

1 Answers1

0

On Android 11+ devices created files will often be scannd by the MediaStore directly.

But on deleting the file the MediaStore mostly is not informed.

Hence if you try to create a file with the same name again the MediaStore gets involved and wil report that the file already exists.

Remove the MediaStore entry first.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Thanks for the info, how to delete MediaStore entry , any code sample ? – prat Feb 11 '22 at 07:30
  • Please read the stackoverflow pages tagged `mediastore`. Next time you try to dele a file delete it using the MediaStore. – blackapps Feb 11 '22 at 07:32