W/System.err: java.io.FileNotFoundException:ENOENT (No such file or directory)

Viewed 381

Not sure how to correct this error. I tried changing the manifest file but still didn't get any result. Also tried using various methods suggested in the site. But it didn't work as well. I don't know if it's about the sdk version as I'm using Android 12. But the same code still worked on the tutorial.

[![Manifest file][1]][1]

[![Manifest][2]][2]


        //The Save button
        val saveButton:ImageButton = findViewById(R.id.ib_save)
        saveButton.setOnClickListener{

            if(isReadStorageAllowed()){
                val fl_container:FrameLayout = findViewById(R.id.fl_container)
                lifecycleScope.launch{
                    saveFileFromBitmap(getBitmapFromView(fl_container))
                }
            }
        }

The error messages in the log keeps pointing to this code:

private suspend fun  saveFileFromBitmap(mBitMap:Bitmap?): String{

        var result = ""
        withContext(Dispatchers.IO){
            if(mBitMap != null){
                try {
                    val byte = ByteArrayOutputStream()
                    mBitMap.compress(Bitmap.CompressFormat.PNG,90, byte)


                    val f = File(externalCacheDir?.absoluteFile.toString() +
                            File.pathSeparator  +
                            "KidsDrawingApp_" + System.currentTimeMillis()/1000, ".jpg")
                    val fo = FileOutputStream(f)
                    fo.write(byte.toByteArray())
                    fo.close()

                    result = f.absolutePath

                    runOnUiThread{
                        if (!result.isEmpty()){
                            Toast.makeText(this@MainActivity,
                                "Saved Successfully + $result",Toast.LENGTH_SHORT).show()
                        }else{
                            Toast.makeText(this@MainActivity,
                                "Something went wrong while saving", Toast.LENGTH_SHORT).show()
                        }
                    }

                }
                catch (e:Exception){
                    result = ""
                    e.printStackTrace()
                }
            }
        }

            return  result

    }```

Stacktrace:
2021-12-10 15:00:56.448 19068-19145/com.example.drawingapp W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.example.drawingapp/cache:KidsDrawingApp_1639128656/.jpg: open failed: ENOENT (No such file or directory)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:575)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at com.example.drawingapp.MainActivity$saveFileFromBitmap$2.invokeSuspend(MainActivity.kt:171)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
2021-12-10 15:00:56.449 19068-19145/com.example.drawingapp W/System.err:     at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
2021-12-10 15:00:56.450 19068-19145/com.example.drawingapp W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
2021-12-10 15:00:56.450 19068-19145/com.example.drawingapp W/System.err:     at libcore.io.Linux.open(Native Method)
2021-12-10 15:00:56.450 19068-19145/com.example.drawingapp W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:567)
2021-12-10 15:00:56.450 19068-19145/com.example.drawingapp W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273)
2021-12-10 15:00:56.451 19068-19145/com.example.drawingapp W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:567)
2021-12-10 15:00:56.451 19068-19145/com.example.drawingapp W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7725)
2021-12-10 15:00:56.451 19068-19145/com.example.drawingapp W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:561)
2021-12-10 15:00:56.451 19068-19145/com.example.drawingapp W/System.err:    ... 9 more

I cannot figure out what the problem is with this code and I've searched all the internet. Please help me out and thanks in advance.


  [1]: https://i.stack.imgur.com/J1eZI.png
  [2]: https://i.stack.imgur.com/o5XWu.png
1 Answers

Look at the first line in your stacktrace:

java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.example.drawingapp/cache:KidsDrawingApp_1639128656/.jpg: open failed: ENOENT (No such file or directory)

It's telling you the path of the file you're trying to open, and KidsDrawingApp_1639128656/.jpg (i.e. a file called .jpg in a directory called KidsDrawingApp_1639128656) probably isn't what you wanted, right?

In your File constructor you're passing two Strings as parameters - here's what that one does:

Otherwise the parent pathname string is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file.

You probably just wanted to append .jpg to the path you're building, instead of passing it as the child parameter

"KidsDrawingApp_" + System.currentTimeMillis()/1000 + ".jpg"
// or you can do this in Kotlin
"KidsDrawingApp_${System.currentTimeMillis()/1000}.jpg"
Related