0

I have an application and some events i want to keep them inside a txt file in the Downloads. i have the following piece of code but i get an exception (Permission denied). I have Android 10 Samsung S9 and i have granted all the permissions (write external storage)

 val externalStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            val myFile = File(externalStorageDir, "mylog.txt");

            if (myFile.exists()) {
                try {
                    val fostream = FileOutputStream(myFile);
                    val oswriter = OutputStreamWriter(fostream);
                    val bwriter = BufferedWriter(oswriter);
                    bwriter.write("Hi James. This is a message");
                    bwriter.newLine();
                    bwriter.close();
                    oswriter.close();
                    fostream.close();
                } catch (e: IOException) {
                    e.printStackTrace();
                }
            } else {
                try {
                    myFile.createNewFile();
                } catch (e: IOException) {
                    e.printStackTrace();
                }
            }

The exception i get is this

java.io.IOException: Permission denied
W:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
W:     at java.io.File.createNewFile(File.java:1008)
W:     at package_name.utilities.SystemUtils$Companion.writefile(SystemUtils.kt:188)
W:     at package_name.ui.activities.SplashActivity.onCreate(SplashActivity.kt:37)
W:     at android.app.Activity.performCreate(Activity.java:7955)
W:     at android.app.Activity.performCreate(Activity.java:7944)
W:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
W:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
W:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
W:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
W:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
W:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
W:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
W:     at android.os.Handler.dispatchMessage(Handler.java:107)
W:     at android.os.Looper.loop(Looper.java:237)
W:     at android.app.ActivityThread.main(ActivityThread.java:7811)
W:     at java.lang.reflect.Method.invoke(Native Method)
W:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)

1) Why i cant write inside there? 2) Is there a library that can do that for me?

james04
  • 1,580
  • 2
  • 20
  • 46

1 Answers1

0

add <application android:requestLegacyExternalStorage="true" ... > in you manifest. It's new thing in android Q. for better answer check this: https://stackoverflow.com/a/56821320/11475673

oto
  • 383
  • 4
  • 18