Home > Software design >  java.io.FileNotFoundException open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException open failed: ENOENT (No such file or directory)

Time:06-01

I have a temp file that is made for creating an image from a cropping library and I can see the file in Device File Explorer but when I try to open the file I get this error:

java.io.FileNotFoundException: file:/data/user/0/com.example.demo/cache/.tmp/cropped1651879842159823361.png: open failed: ENOENT (No such file or directory)

This is how that file is created:

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

viewModel.tempPath is just the following:

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

I can see that file got created and is valid, but when I try to access it, it claims it doesn't exists. I simply open the file by doing File(uri.toString()). in the view model

I'm not sure what is wrong and why it can't find the file. If this matters, I'm using an emulator that has google play and it's Android 11.

CodePudding user response:

You need to open the file using new File(uri.getPath()). uri.toString() returns the URI as a string, that means "file://path/to/file" which is not a valid path.

  • Related