试图创建文件

Trying to create file

正在尝试执行此代码

         try {
            if(!file.exists()){
                file.createNewFile()
                file.exists()
            }

            val out = FileOutputStream(file)
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
            out.flush()
            out.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }

但是得到

java.io.IOException: No such file or directory

非常奇怪的行为

已解决

    val file1 = File(Environment.getExternalStorageDirectory().toString() + "/" + File.separator + "test.png")
    file1.createNewFile()
    file1.exists()

    val out = FileOutputStream(file1)
    finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
    out.flush()
    out.close()