Android Studio 如何将imageview保存到图库中?

7cwmlq89  于 2023-08-07  发布在  Android
关注(0)|答案(3)|浏览(148)

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

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

字符串

ds97pgxw

ds97pgxw1#

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

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)

zmeyuzjn

zmeyuzjn2#

我想,这应该行得通:第一个月
还有,这道题里写着:android - save image into gallery

f4t66c6m

f4t66c6m3#

使用下面的函数,它100%工作与目标SDK 30+

fun insertImage(
    cr: ContentResolver,
    source: Bitmap?,
    title: String?,
    description: String?
): String? {
    val values = ContentValues()
    values.put(Images.Media.TITLE, title)
    values.put(Images.Media.DISPLAY_NAME, title)
    values.put(Images.Media.DESCRIPTION, description)
    values.put(Images.Media.MIME_TYPE, "image/jpeg")
    // Add the date meta data to ensure the image is added at the front of the gallery
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis())
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis())
    var url: Uri? = null
    var stringUrl: String? = null /* value to be returned */
    try {
        url = cr.insert(Images.Media.EXTERNAL_CONTENT_URI, values)
        if (source != null) {
            val imageOut = cr.openOutputStream(url!!)
            try {
                source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut)
            } finally {
                imageOut!!.close()
            }
            val id = ContentUris.parseId(url!!)
            // Wait until MINI_KIND thumbnail is generated.
            val miniThumb =
                Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null)
            // This is for backward compatibility.
            storeThumbnail(cr, miniThumb, id, 50f, 50f, Images.Thumbnails.MICRO_KIND)
        } else {
            cr.delete(url!!, null, null)
            url = null
        }
        Toast.makeText(this, "Image Saved to Gallery", Toast.LENGTH_LONG).show()
    } catch (e: Exception) {
        if (url != null) {
            cr.delete(url, null, null)
            url = null
        }
    }
    if (url != null) {
        stringUrl = url.toString()
    }
    return stringUrl
}

private fun storeThumbnail(
    cr: ContentResolver,
    source: Bitmap,
    id: Long,
    width: Float,
    height: Float,
    kind: Int
): Bitmap? {

    // create the matrix to scale it
    val matrix = Matrix()
    val scaleX = width / source.width
    val scaleY = height / source.height
    matrix.setScale(scaleX, scaleY)
    val thumb = Bitmap.createBitmap(
        source, 0, 0,
        source.width,
        source.height, matrix,
        true
    )
    val values = ContentValues(4)
    values.put(Images.Thumbnails.KIND, kind)
    values.put(Images.Thumbnails.IMAGE_ID, id.toInt())
    values.put(Images.Thumbnails.HEIGHT, thumb.height)
    values.put(Images.Thumbnails.WIDTH, thumb.width)
    val url = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI, values)
    return try {
        val thumbOut = cr.openOutputStream(url!!)
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut)
        thumbOut!!.close()
        thumb
    } catch (ex: FileNotFoundException) {
        null
    } catch (ex: IOException) {
        null
    }

字符串

相关问题