Today, I was working with my old program, which I had made in December 2020. Due to some odd reason, I delayed my app development process.
An application was working a few months ago when suddenly the app crashed with the error Exception ‘open failed: EACCES (Permission denied)’.
The application workflow was pretty simple. When the user clicks on the “Share Button” programmatically, the application will take a screenshot and use the explicit intent to send screenshots using any applications that support image share.
I took a paused for a moment and started thinking, What has suddenly happened to the project? If you read the error, it says something is wrong with the permissions.
Instantly, I checked the permissions code and found everything good. When I opened the Android manifest, I found the culprit: “WRITE_EXTERNAL_STORAGE no longer provides write access when targeting Android 10+.”
The problem starts getting clearer.
I open build.gradle and checked target version are changed to API 30 (Android 11) somehow.
As usual, I did research and found that the application target for SDK 29 or later uses scoped storage as the default option.
If you are thinking, What is Scoped Storage? I’ll clarify to you that Scoped Storage sets a limit on the access file.
For example, if my XYZ application is stored in a specific directory location, want to upload photos from any other directory other than the application directory, I’ll not be allowed to access the file according to the new Google policy.
So we’ve got the problem; tell me, Gagan, how to resolve this? You can use the MediaStore method or simply use the Legacy Storage policy.
How to resolve abov error using LegacyStorage?
We need to pass the single line of code, which will turn off the flag for Android 10+ devices, and you will be allowed to use LegacyStorage.
Go to the project pane, click on AndroidManifest, and add the highlighted code inside <application>.
<manifest ... > <application ... android:requestLegacyExternalStorage="true" ..> ... </application> </manifest>
Changes should be like this below sample image.
Once you add requestLegacyExternalStorage = “true” under the <application> tag. That’s all for Android 10 or 11 users. You can test your application.
For Android 12, users need to add one more tag in AndroidManifest.xml that is “android:exported=”true” for the MainActivity.
Run the application and your application functions will start working again.
That’s it to resolve Exception ‘open failed: EACCES (Permission denied)’. If you are still facing any issues while following this guide, please let us know in the comment section.
For your ease, we have uploaded a sample project to the GitHub repository. To clone the repository click on this link.
What are your thoughts about this article?
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.