java.io.FileNotFoundException打开失败:ENOENT(没有这样的文件或目录)

问题描述 投票:0回答:2

我有一个临时文件,用于从裁剪库创建图像,我可以在设备文件资源管理器中看到该文件,但当我尝试打开该文件时,出现此错误:

java.io.FileNotFoundException:文件:/data/user/0/com.example.demo/cache/.tmp/cropped1651879842159823361.png:打开失败:ENOENT(没有这样的文件或目录)

这就是该文件的创建方式:

val croppedImageFile = File.createTempFile("cropped", ".png", viewModel.tempPath)
val destinationUri = Uri.fromFile(croppedImageFile)

viewModel.tempPath 如下:

viewModel.tempPath = "${this.cacheDir}/.tmp"

我可以看到该文件已创建并且有效,但是当我尝试访问它时,它声称它不存在。我只需通过执行

File(uri.toString())
打开文件即可。在视图模型中

我不确定出了什么问题以及为什么找不到该文件。如果这很重要,我使用的是带有 google play 的模拟器,并且是 Android 11。

android kotlin android-fileprovider ucrop
2个回答
3
投票

您需要使用

new File(uri.getPath())
打开文件。

uri.toString()
以字符串形式返回 URI,这意味着“file://path/to/file”不是有效路径。


0
投票

java.io.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录) 在 android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:151) 在 android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:836) 在 android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2045) 在 android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1860) 在 android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1775) 在 m6.k.h(源文件:29) 在 m6.k.g(源文件:251) 在 m6.j.k(源文件:13) 在 a8.a.E(来源不明:8) 在 q8.c0.run(来源不明:107) 在 l.h.run(源文件:661) 在 w8.i.run(来源不明:2) 在 w8.a.run(源文件:94)

© www.soinside.com 2019 - 2024. All rights reserved.