如何将 ImageView 中的图像保存到图库

发布于 2025-01-10 23:12:55 字数 2134 浏览 3 评论 0原文

我正在尝试将相机活动中拍摄的图像保存到图库中,因此我为其创建了一个功能来执行相同的操作。虽然应用程序正在运行,但图像没有保存在图库中。任何人都可以帮忙吗?

在这里我在 kotlin 中创建了这个函数

 private fun saveImageToGallery(bitmap: Bitmap) {

    val file: File = Environment.getExternalStorageDirectory()
    val dir = File(file.absolutePath + "/MyImageEditorTasks/")
    dir.mkdirs()
    val filename = String.format("${System.currentTimeMillis()}.jpeg")
    val outfile = File(dir, filename)
    var outputstream: FileOutputStream? = null
    try {
        outputstream = FileOutputStream(outfile)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputstream)
        outputstream.flush()
        outputstream.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }

}

在这里我使用了相同的函数

 public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == GALLERY) {
            if (data != null) {
                val contentURI = data.data
                try {
                    // Here this is used to get an bitmap from URI
                    val selectedImageBitmap =
                        MediaStore.Images.Media.getBitmap(this.contentResolver, contentURI)
                    saveImageToInternalStorage(selectedImageBitmap)
                    preview!!.setImageBitmap(selectedImageBitmap) // Set the selected image from GALLERY to imageView.
                } catch (e: IOException) {
                    e.printStackTrace()
                    Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show()
                }
            }
            // TODO (Step 7: Camera result will be received here.)
        } else if (requestCode == CAMERA) {
            val thumbnail: Bitmap = data!!.extras!!.get("data") as Bitmap                 
            saveImageToGallery(thumbnail)
            preview!!.setImageBitmap(thumbnail) // Set to the imageView.
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        Log.e("Cancelled", "Cancelled")
    }
}

I am trying to save a taken image from the camera activity to the gallery so i made a function for it to do the same.Though the app is running but the image is not getting saved in the gallery.Can Anyone Help, please.

here I created the function in kotlin

 private fun saveImageToGallery(bitmap: Bitmap) {

    val file: File = Environment.getExternalStorageDirectory()
    val dir = File(file.absolutePath + "/MyImageEditorTasks/")
    dir.mkdirs()
    val filename = String.format("${System.currentTimeMillis()}.jpeg")
    val outfile = File(dir, filename)
    var outputstream: FileOutputStream? = null
    try {
        outputstream = FileOutputStream(outfile)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputstream)
        outputstream.flush()
        outputstream.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }

}

Here I used the same function

 public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == GALLERY) {
            if (data != null) {
                val contentURI = data.data
                try {
                    // Here this is used to get an bitmap from URI
                    val selectedImageBitmap =
                        MediaStore.Images.Media.getBitmap(this.contentResolver, contentURI)
                    saveImageToInternalStorage(selectedImageBitmap)
                    preview!!.setImageBitmap(selectedImageBitmap) // Set the selected image from GALLERY to imageView.
                } catch (e: IOException) {
                    e.printStackTrace()
                    Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show()
                }
            }
            // TODO (Step 7: Camera result will be received here.)
        } else if (requestCode == CAMERA) {
            val thumbnail: Bitmap = data!!.extras!!.get("data") as Bitmap                 
            saveImageToGallery(thumbnail)
            preview!!.setImageBitmap(thumbnail) // Set to the imageView.
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        Log.e("Cancelled", "Cancelled")
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-01-17 23:12:55

您可以从 Drawble 获取并将其保存到您的画廊。函数示例如下。

private fun saveImage(drawable: Drawable) {
    val file = getDisc()

    if (!file.exists() && !file.mkdirs()) {
        file.mkdir()
    }

    val simpleDateFormat = SimpleDateFormat("yyyymmsshhmmss")
    val date = simpleDateFormat.format(Date())
    val name = "IMG" + date + ".jpg"
    val fileName = file.absolutePath + "/" + name
    val newFile = File(fileName)

    try {
        val draw = drawable as BitmapDrawable
        val bitmap = draw.bitmap
        val fileOutPutStream = FileOutputStream(newFile)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutPutStream)
        Toast.makeText(requireContext(), "File saved succesfully", Toast.LENGTH_SHORT)
            .show()
        savedFile = newFile
        fileOutPutStream.flush()
        fileOutPutStream.close()

    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }

}

private fun getDisc(): File {
    val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
    return File(file, "YOUR_ALBUM_NAME")
}

You can get from drawble and save it to your gallery. Function example is below.

private fun saveImage(drawable: Drawable) {
    val file = getDisc()

    if (!file.exists() && !file.mkdirs()) {
        file.mkdir()
    }

    val simpleDateFormat = SimpleDateFormat("yyyymmsshhmmss")
    val date = simpleDateFormat.format(Date())
    val name = "IMG" + date + ".jpg"
    val fileName = file.absolutePath + "/" + name
    val newFile = File(fileName)

    try {
        val draw = drawable as BitmapDrawable
        val bitmap = draw.bitmap
        val fileOutPutStream = FileOutputStream(newFile)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutPutStream)
        Toast.makeText(requireContext(), "File saved succesfully", Toast.LENGTH_SHORT)
            .show()
        savedFile = newFile
        fileOutPutStream.flush()
        fileOutPutStream.close()

    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }

}

private fun getDisc(): File {
    val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
    return File(file, "YOUR_ALBUM_NAME")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文