首页 > 解决方案 > android.system.ErrnoException: open failed: ENOENT (No such file or directory), 该文件存在于同名路径中

问题描述

我正在尝试将图像发送到我的 apollo-graphql 服务器,但出现以下错误:

android.system.ErrnoException:打开失败:ENOENT(没有这样的文件或目录)

这是安卓代码:

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)
                }

图片路径:

/document/1804-2E16:DCIM/definicion-de-persona-min.jpg:打开失败:ENOENT(没有这样的文件或目录)

应用权限:

<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" />

提前致谢。

标签: androidandroid-studiofilekotlinapollo-android

解决方案


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


推荐阅读