Exception 'open failed: EACCES (Permission denied)' on Android

前端 未结 30 1593
[愿得一人]
[愿得一人] 2020-11-22 00:59

I am getting

open failed: EACCES (Permission denied)

on the line OutputStream myOutput = new FileOutputStream

30条回答
  •  花落未央
    2020-11-22 01:45

    To store a file in a directory which is foreign to the app's directory is restricted above API 29+. So to generate a new file or to create a new file use your application directory like this :-

    So the correct approach is :-

    val file = File(appContext.applicationInfo.dataDir + File.separator + "anyRandomFileName/")
    

    You can write any data into this generated file !

    The above file is accessible and would not throw any exception because it resides in your own developed app's directory.

    The other option is android:requestLegacyExternalStorage="true" in manifest application tag as suggested by Uriel but its not a permanent solution !

提交回复
热议问题