Android 保存图片并刷新相册(无需权限)

一、背景

之前的保存图片吧,在Android11之前,我会去申请WIRITE权限,11之后,我又去申请MANAGE_EXTERNAL_STORAGE这个权限,该权限将授权读写所有共享存储内容,这也将同时包含非媒体类型的文件。但是在google上架的时候,MANAGE_EXTERNAL_STORAGE属于敏感权限,需要向谷歌做声明,做了,没过,谷歌说可以不要这个权限也可以保存,看来是我无知了,于是去研究了一下下,有了下面的方法。

二、实现

    @SuppressLint("UseCompatLoadingForDrawables")
    @AfterPermissionGranted(200)
    fun saveFreshAppImageToGallery(context: PreViewActivity, imageResId: Int) {
        val drawable = context.getDrawable(imageResId)

        if (drawable is BitmapDrawable) {
            val bitmap = drawable.bitmap
            val displayName = "${System.currentTimeMillis()}.jpg"

            val values = ContentValues().apply {
                put(MediaStore.Images.Media.DISPLAY_NAME, displayName)
                put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
                put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000)
            }

            val contentResolver = context.contentResolver
            val collection =
                MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
            val item = contentResolver.insert(collection, values)

            item?.let { uri ->
                try {
                    val outputStream = contentResolver.openOutputStream(uri)
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
                    outputStream?.close()
                    Toast.makeText(context, "Save successfully!", Toast.LENGTH_SHORT).show()
                    val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                    mediaScanIntent.data = uri
                    context.sendBroadcast(mediaScanIntent)
                } catch (e: Exception) {
                    Toast.makeText(context, "Save Failed!", Toast.LENGTH_SHORT).show()
                    e.printStackTrace()
                }
            }
        }
    }

其中:通知相册刷新的逻辑是下面代码

val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
mediaScanIntent.data = uri
context.sendBroadcast(mediaScanIntent)

或者下面的方法

    private fun saveToGallery(photoFile: File) {
        val contentValues = ContentValues().apply {
            put(MediaStore.Images.Media.DISPLAY_NAME, "photo.jpg")
            put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                put(MediaStore.Images.Media.IS_PENDING, 1)
            }
        }

        val contentResolver = contentResolver
        val collectionUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
        } else {
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        }

        val imageUri = contentResolver.insert(collectionUri, contentValues)

        imageUri?.let { uri ->
            try {
                contentResolver.openOutputStream(uri)?.use { outputStream ->
                    val inputStream = FileInputStream(photoFile)
                    inputStream.copyTo(outputStream)
                    inputStream.close()
                    outputStream.close()
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    contentValues.clear()
                    contentValues.put(MediaStore.Images.Media.IS_PENDING, 0)
                    contentResolver.update(uri, contentValues, null, null)
                }
            } catch (e: IOException) {
            }
        }
    }

原理都是一样的

你可能感兴趣的:(android)