我需要保存一些编辑过的图像。我申请编辑正在运行并显示在ImageView中的ColorFilter(https://medium.com/over-engineering/manipulating-images-and-drawables-with-androids-colorfilter-25bf061843e7)。下一步是保存这些图像。要保存它,我正在使用:
val wrapper = ContextWrapper(applicationContext)
var imagesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
var projectDir = File(imagesDir, UUID.randomUUID().toString())
projectDir.mkdirs()
var image = File(projectDir, "$name.jpg")
try
{
val stream: OutputStream = FileOutputStream(image)
ImageContainer.convertImageViewToBitmap(imageViewSuccess).compress(Bitmap.CompressFormat.JPEG, 100, stream)
stream.flush()
stream.close()
}
catch (e: IOException)
{
e.printStackTrace()
}
[...]
,但是如果我使用功能3,则不会随即保存所应用的ColorFilter。功能2已弃用。功能1使用滤镜,但还会重新缩放图像,如果ImageView不可见,该功能将无法正常工作。
//1
fun convertImageViewToBitmap(view: ImageView): Bitmap
{
val bitmap = Bitmap.createBitmap(
view.width,
view.height, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
return bitmap
}
//2
fun convertImageViewToBitmap(v: ImageView): Bitmap
{
v.isDrawingCacheEnabled = true
val bitmap = Bitmap.createBitmap(v.getDrawingCache())
v.isDrawingCacheEnabled = false
return bitmap
}
//3
fun convertImageViewToBitmap(v: ImageView): Bitmap
{
return (v.drawable as BitmapDrawable).bitmap
}
如何在位图上使用ColorFilter并将其保存到存储中?