-1

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.

  • 2
    The error is not in this code ... the real problem is that you are using wrong code to get path from uri... as the only good way is: ***to create new file and copy content from uri to this file and use its path*** (or use content directly) ... similar questions are asked again and again because some dudes give +1 for wrong answers – Selvin Oct 09 '20 at 09:53

1 Answers1

0

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

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Manually testing the path, I get the same error: File("content://com.android.externalstorage.documents/document/1804-2E16:DCIM/definicion-de-persona-min.jpg") – AlejandroOO Oct 09 '20 at 11:12
  • this is NOT a file path! this is `URI` pointing on a file, which may be stored anywhere, so it can be converted only to a stream. `File` object may be created only from `URI` starting with `file://`. and no, manually replacing `content://` to `file://` won't fix this. seems like you haven't read my answer at all and for shure didn't read linked doc and topic on SO... – snachmsm Oct 09 '20 at 11:33