Skip to content

FileAccess Error when opening document file on Android. Only works using MANAGE_EXTERNAL_STORAGE permission. #105328

Closed as not planned
@edoartworks

Description

@edoartworks

Tested versions

  • Reproducible in v4.4.1.stable.official [49a5bc7]
  • Not reproducible in any earlier version because the Native FileDialog for Android was introduced in 4.4
  • Happens on both Gradle and non-Gradle builds

System information

v4.4.1.stable - Samsung Galaxy S23 (SM-S9110) - Android 14

Issue description

On Android, I want to select a .txt file from the phone storage, using the native FileDialog.
This works fine and I'm able to get the file path correctly (although with a caveat. More info at the end).

In my test case, the example text file is located in: /storage/emulated/0/Documents/test.txt

Then, I want to load that file using: FileAccess.open(file_path, FileAccess.READ)
This is where I get ERROR 7: ERR_FILE_NOT_FOUND

This happens when exporting with no permissions. Because according to android documentation... my app shouldn't need any file access permission to access that file.

From that page:

Because the user is involved in selecting the files or directories that your app can access, this mechanism doesn't require any system permissions, and user control and privacy is enhanced.

Instead, when I enable MANAGE_EXTERNAL_STORAGE permission, the file is loaded correctly.
But I definitely don't want to use this permission, because any user installing my app could be questioning why on earth it's asking for full file access 😅

I would expect the file to load without setting any special permission.
Unless I'm missing something?


Something else I don't understand is that, using Native FileDialog, seems like I'm restricted to selecting files only in certain folders, like /Documents or /Download.
If I try to load any file from other folders, it simply doesn't trigger the FileDialog file_selected signal (but it still does with MANAGE_EXTERNAL_STORAGE permission enabled) 🤔

Steps to reproduce

  1. Add a hidden FileDialog to your scene, and a button to show it.
  2. On the FileDialog, make sure to enable use_native_dialog and set Access = FileSystem
  3. Bind the FileDialog _on_file_dialog_file_selected signal
  4. Bind the button _on_button_pressed signal
  5. Use this code to open the Dialog and Open the file
  6. Deploy on Android device and observe

This is my code:

extends Control

func _ready() -> void:
	OS.request_permissions()


func _on_file_dialog_file_selected(path: String) -> void:
	print("Selected file: ", path)
	print("File exists? ", FileAccess.file_exists(path))
	
	var file = FileAccess.open(path, FileAccess.READ)
	if file:
		print(file.get_as_text())
	else:
		print("ERROR opening file: ")
		print(FileAccess.get_open_error())


func _on_button_pressed() -> void:
	$FileDialog.popup()

Minimal reproduction project (MRP)

FileDialogTest.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions