【问题标题】:How do I save an imageview into the gallery?如何将图像视图保存到图库中?
【发布时间】:2022-01-23 18:01:57
【问题描述】:

我想点击一个按钮,将图像视图保存到 kotlin 中的 android 设备库。我有这个

val image = findViewById<ImageView>(R.id.QRImage)

【问题讨论】:

  • val image = ... 那应该是val imageView = ...。而且您不能将图像视图存储到画廊,而只能存储 imsges。

标签: android android-studio kotlin kotlin-android-extensions


【解决方案1】:

您可以先将图像转换为位图,如下面的代码:

val image = findViewById<ImageView>(R.id.QRImage)
val imageBitmap = image.drawable.toBitmap()

然后将位图保存在您想要的位置,如下面的代码:

val fileOutputStream = FileOutputStream("Location") //location of the image 
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)

或者像下面的代码一样直接保存:

 MediaStore.Images.Media.insertImage(context.contentResolver, imageBitmap, "Image title ", null)

【讨论】:

  • 所以保存到 dcim 目录将是
  • Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
  • 是的,您还可以在下面的链接中查看如何将图像保存在 dcim 目录中的示例。 stackoverflow.com/questions/48822182/…
【解决方案2】:

我认为,这应该可行: MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

另外,写在这个问题里:android - save image into gallery

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多