如何将 ImageView 中的图像保存到图库
我正在尝试将相机活动中拍摄的图像保存到图库中,因此我为其创建了一个功能来执行相同的操作。虽然应用程序正在运行,但图像没有保存在图库中。任何人都可以帮忙吗?
在这里我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从 Drawble 获取并将其保存到您的画廊。函数示例如下。
You can get from drawble and save it to your gallery. Function example is below.