android.system.ErrnoException: open failed: ENOENT (No such file or directory), the file exists in the path with the name

1.6k views Asked by At

I am trying to send an image to my apollo-graphql server, but I get the following error:

android.system.ErrnoException: open failed: ENOENT (No such file or directory)

This is the Android code:

lifecycleScope.launch {
                    val response = try {
                        apolloClient(requireContext()).mutate(
                            SubirImagenMutation(
                                imagenData = FileUpload("image/jpg", File(imagen.path!!).toString())
                            )
                        ).toDeferred().await()
                    } catch (e: ApolloException) {
                        Toasts().visualizarToast(requireContext(), getString(R.string.str_err_generico))
                        return@launch
                    }
                    findNavController().navigate(R.id.serviciosFragment)
                }

The image path:

/document/1804-2E16:DCIM/definicion-de-persona-min.jpg: open failed: ENOENT (No such file or directory)

App permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Thanks in advance.

1

There are 1 answers

2
snachmsm On

not every URI can be resolved as strict file path on file system. e.g. some file pickers can return file placed on some cloud storage...

in your case URI points on /document/1804-2E16:DCIM/definicion-de-persona-min.jpg, which is malformed for shure. you are probably getting URI similar to this /document/1804-2E16 thinking that this is directory path, so you are adding String filename, but this isn't how this works... your real original and full "path" to file looks probably like this:

content://com.android.providers.downloads.documents/document/1804-2E16

no filename, no fileformat, just a "pointer" - an URI

read about FileProviders and check out THIS topic for some info how to obtain access to such files. fixing this "bug" isn't trivial, in short you have to handle some file streams