Android 工作室图像保存到外部存储

Android studio image saving to external storage

所以我创建了一个功能来使用应用程序的名称将照片保存到图库,一切正常,我在应用程序中处理其他内容(与此功能无关)然后当我想再次使用这个功能它停止工作,这是我的代码

 fun saveImage(itemImage: View, activity: Activity) {
    var fileName: String
    val imageFromView = getBitmapFromView(itemImage)

    ByteArrayOutputStream().apply {
        Bitmap.createBitmap(imageFromView).compress(Bitmap.CompressFormat.JPEG, 100, this)
        fileName = UUID.nameUUIDFromBytes(this.toByteArray()).toString().replace("-", "")
    }

    val imageFile = File("${Environment.getExternalStorageDirectory().absolutePath}/ChatOut/$fileName.jpg/")
    val direct = File("${Environment.getExternalStorageDirectory().absolutePath}/ChatOut/").apply {
        if (!exists())
            mkdirs()
    }

    if (!imageFile.exists()) {
        File(direct, "$fileName.jpg").apply {
            FileOutputStream(this).apply {
                Bitmap.createBitmap(imageFromView).compress(Bitmap.CompressFormat.JPEG, 100, this)
                flush()
                close()
            }
        }.let {
            activity.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    ContentValues().apply {
                        put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
                        put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
                        put(MediaStore.Images.Media.DATA, it.absolutePath)
                    }
            )
       }

       Toast.makeText(activity, "saved", Toast.LENGTH_SHORT).show()
    } else
       Toast.makeText(activity, "Already saved", Toast.LENGTH_SHORT).show()
}

此函数检查图像是否已保存,如果没有则保存它,我得到一个

/storage/emulated/0/ChatOut/0fe4706621ce318fb4e7292e16bcfb17.jpg: open failed: ENOENT (No such file or directory)

我的清单中有使用许可

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我确保也授予了权限。

出于某种原因,无论我做什么 mkdirs() returns 错误,它都无法成功创建文件或目录...

请帮忙,感谢您抽出宝贵时间。

我已经通过应用以下代码解决了我的问题,以防其他人遇到同样的问题

fun saveImage(itemImage: View, activity: Activity) {
    val fileName: String
    val imageFromView = getBitmapFromView(itemImage)

    ByteArrayOutputStream().apply {
        imageFromView.compress(Bitmap.CompressFormat.JPEG, 100, this)
        fileName = UUID.nameUUIDFromBytes(this.toByteArray()).toString().replace("-", "")
    }

    val imageFile =  File("${activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)}/ChatOut/$fileName.jpg/")

    if (!imageFile.exists()) {

        val contentResolver = ContentValues().apply {
            put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
            put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
            put(MediaStore.Images.Media.DATA, imageFile.absolutePath)
        }

         activity.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentResolver).apply {
              imageFromView.compress(Bitmap.CompressFormat.JPEG, 100, activity.contentResolver.openOutputStream(this!!))
         }


        Toast.makeText(activity, "saved", Toast.LENGTH_SHORT).show()
     } else
        Toast.makeText(activity, "Already saved", Toast.LENGTH_SHORT).show()
}



fun getBitmapFromView(view: View): Bitmap {
    return Bitmap.createBitmap(view.width, view.height,Bitmap.Config.ARGB_8888).apply {
        Canvas(this).apply {
            view.draw(this)
        }
    }
}