0

I have been struggling to find a way which will save logcat to file using Scoped Access framework. I have gone through the below link SAF link, but no luck.

Till Android 10, I was using "requestLegacyExternalStorage" to make it work with the below code.

 public void createLogs() {
    if (isExternalStorageWritable()) {
        File appDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/c4cex");
        File logDirectory = new File(appDirectory + "/logs");

        File logFile = new File(logDirectory, "c4cex.txt");
        //File preLogonLogFile = new File(logDirectory, "preLogonLogs.txt");

        // create app folder
        if (!appDirectory.exists()) {
            appDirectory.mkdir();
        }
        // create log folder
        if (!logDirectory.exists()) {
            logDirectory.mkdir();
        }
        try {

            //  Process process = Runtime.getRuntime().exec("logcat -f  " + logFile);
            Process process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r 2048 -n 4"); // -r amount of bits to write, -n amount of logs to rotate
            //preLogonLogFile.createNewFile();
            //Process process1 = Runtime.getRuntime().exec("logcat -f " + preLogonLogFile + " -r 2048 -n 5"); // -r amount of bits to write, -n amount of logs to rotate

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

After getting the logs in a file , I want to share it through email and hence SAF approach will only work as per the Android documents.

prat
  • 597
  • 8
  • 17
  • "I want to share it through email" -- use `FileProvider`, as shown [in the documentation for sharing files](https://developer.android.com/training/secure-file-sharing). "SAF approach will only work" -- you can store the content in filesystem locations that your process can write to (e.g., `getFilesDir()`) and use `FileProvider`. – CommonsWare Nov 18 '21 at 12:14
  • I am using email composer plugin https://github.com/katzer/cordova-plugin-email-composer – prat Nov 18 '21 at 12:23
  • Find some Cordova plugin that lets you share using `FileProvider`. If you cannot find such a plugin, I guess you need to write one. I have not used Cordova in a nearly a decade, and so I am not familiar with the current lineup of plugins. The particular plugin that you linked to appears to be unmaintained, as its code has not been updated in nearly three years. – CommonsWare Nov 18 '21 at 12:27
  • the mentioned plugin link for emailing already uses FileProvider – prat Nov 18 '21 at 12:29
  • Better: `if (!logDirectory.exists()) { if ( !logDirectory.mkdirs()) return; }` . You only need one mkdirs() call. Not two times mkdir(). And always check return value and stop if false. – blackapps Nov 18 '21 at 12:33
  • You can use the same code to create that file on Android 11. Well.. i see no reason that it should not work. – blackapps Nov 18 '21 at 12:34
  • "the mentioned plugin link for emailing already uses FileProvider" -- it uses an out of date version from the Android Support Library, and that may cause problems with combined with other plugins. It also does not appear to call `FileProvider.getUriForFile()` for you, so you would need to do that somewhere in your code and supply that `Uri` to the plugin. – CommonsWare Nov 18 '21 at 12:36
  • @blackapps, 'getExternalStoragePublicDirectory' wont be supported from Android 12. Also c4cex.txt not getting created inside logs file using the above approach. – prat Nov 18 '21 at 12:43
  • Yes it is supported again. CommonsWare reported that already two times. Please try it. It was always supported only deprecated but it worked always. – blackapps Nov 18 '21 at 12:53
  • Just tried that code on an Android 12 emulator and it works perfectly. – blackapps Nov 18 '21 at 12:55
  • `Saving logcat to a file using SAF ...` I wonder why you did not mention that in `"logcat -f " + logFile + .....` you did not know how to use a SAF uri there. And otherwise i do not understand your SAF problem. – blackapps Nov 18 '21 at 12:58
  • `"logcat -f " + logFile + ....` You write `logFile` but probably you need `logFile.getAbsolutePath()`. – blackapps Nov 18 '21 at 13:00
  • @blackapps, uri is the issue. Also I could see c4cex.txt file getting generated when checking in Android Device file explorer but in real device, its not appearing – prat Nov 18 '21 at 13:25
  • Of course it is there. Do you think Android Device Explorer is kidding? – blackapps Nov 18 '21 at 13:27
  • My bad, i checked in the wrong device storage – prat Nov 18 '21 at 20:10
  • I could only see dir c4cex-->logs --> empty, ideally it should contain c4c txt file also – prat Nov 18 '21 at 20:11

0 Answers0