Android 保存 bitmap 到共享目录
hefengbao 发布于 2023.10.27 ,最后更新于 2023.10.27
object FileUtil {
fun saveImageToStorage(
context: Context,
bitmap: Bitmap,
filename: String,
mimeType: String = "image/jpeg",
directory: String = Environment.DIRECTORY_PICTURES,
mediaContentUri: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
): Boolean {
val contentResolver: ContentResolver = context.contentResolver
val imageOutStream: OutputStream
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, filename)
put(MediaStore.Images.Media.MIME_TYPE, mimeType)
put(MediaStore.Images.Media.RELATIVE_PATH, directory)
}
contentResolver.run {
val uri =
contentResolver.insert(mediaContentUri, values)
?: return false
imageOutStream = openOutputStream(uri) ?: return false
}
} else {
val imagePath = Environment.getExternalStoragePublicDirectory(directory).absolutePath
val image = File(imagePath, filename)
imageOutStream = FileOutputStream(image)
}
imageOutStream.use { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it) }
return true
}
}
使用示例:
val localDateTime = LocalDateTime.now()
val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
if (FileUtil.saveImageToStorage(
context,
imageBitmap,
"jingmo_${dateTimeFormatter.format(localDateTime)}.jpg"
)
) {
Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show()
}
参考:
https://zhuanlan.zhihu.com/p/172493773
有 0 条评论
发表评论
您的电子邮箱地址不会被公开。 必填项已用 * 标注