1. Background
Save pictures before. Before Android 11, I would apply for the WIRITE permission. After 11, I would apply for the MANAGE_EXTERNAL_STORAGE permission. This permission will authorize reading and writing all shared storage content, which will also include non-media type files. But when Google put it on the shelves, MANAGE_EXTERNAL_STORAGE was a sensitive permission, and I needed to make a statement to Google. I did it, but it failed. Google said that I could save it without this permission. It seemed that I was ignorant, so I did some research and found out. The following method.
2. Realization
@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()
}
}
}
}
Among them: the logic of notifying the album to refresh is the following code
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
mediaScanIntent.data = uri
context.sendBroadcast(mediaScanIntent)
Or the following method
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) {
}
}
}
The principles are the same