I am accessing text file in android but the permission denied errno13 showed up and i have already given all permission of storage
Asked
Active
Viewed 2,977 times
1
-
Do you target Android 11? Android no longer permit access to external storage, unless you're developing a file manager. If it's for educational purposes, you can declare All Files Access permission (be a file manager) – Shlomi Katriel Jul 06 '21 at 05:34
-
and this permission is already declared (`MANAGE_EXTERNAL_STORAGE`), but is it granted? @op check in system settings of your app – snachmsm Jul 06 '21 at 05:41
-
Declaring permissions in manifest was the old way. Those permissions might also need to be asked at runtime as [runtime permissions](https://developer.android.com/training/permissions/requesting) in modern Android versions. – Markus Kauppinen Jul 06 '21 at 07:46
3 Answers
0
From the Chaquopy FAQ:
Since API level 29, Android has a scoped storage policy which prevents direct access to external storage, even if your app has the READ_EXTERNAL_STORAGE permission. Instead, you can use the system file picker, and pass the file to Python as a byte array:
val REQUEST_OPEN = 0
fun myMethod() {
startActivityForResult(
Intent(if (Build.VERSION.SDK_INT >= 19) Intent.ACTION_OPEN_DOCUMENT
else Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
setType("*/*")
}, REQUEST_OPEN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_OPEN && resultCode == RESULT_OK) {
val uri = data!!.data!!
// For Java, see https://stackoverflow.com/a/10297073
val content = contentResolver.openInputStream(uri)!!.use { it.readBytes() }
myPythonModule.callAttr("process", content)
}
}
The Python function can then access the file content however you like:
def process(content):
# `content` is already a bytes-like object, but if you need a standard bytes object:
content = bytes(content)
# If you need a file-like object:
import io
content_file = io.BytesIO(content)
# If you need a filename (less efficient):
import tempfile
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(content)
filename = temp_file.name # Valid only inside the `with` block.

mhsmith
- 6,675
- 3
- 41
- 58
0
it's better to use below permissions if just want to access videos and images :
"android.permission.READ_MEDIA_IMAGES"
"android.permission.READ_MEDIA_VIDEO"
instead of using MANAGE_EXTERNAL_STORAGE

Mosayeb Masoumi
- 481
- 4
- 10
-1
In addition to obtaining these permissions (READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE)during the initial run of the program, you must add the following code to the AndroidManifest.xml file in tag.
android: requestLegacyExternalStorage = "true"

SamanSepahvand
- 86
- 1
- 8
-
1Google changes their privacy policy's since 1st of May 2021. After this Google Play Store will not accept this '(android: requestLegacyExternalStorage = "true")'. – Sanjay Kumaar Jul 06 '21 at 06:48
-
1