问题描述
我通过执行以下操作将图像保存到存储中:
private fun finallySave() {
if (imageView.drawable != null) {
scopeForSaving.launch { savetoStorage() }
} else {
Toast.makeText(this,"Please load image first",Toast.LENGTH_SHORT).show()
}
}
private suspend fun savetoStorage() {
var outFile: File? = null
withContext(@R_502[email protected]) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val resolver = [email protected]
val contentValues = ContentValues().apply {
put(
MediaStore.MediaColumns.@R_502_6422@PLAY_NAME,"Draw_Easy " + System.currentTimeMillis() + ".png"
)
put(MediaStore.MediaColumns.MIME_TYPE,"image/jpeg")
put(MediaStore.MediaColumns.RELATIVE_PATH,"DCIM/Draw Easy")
}
val uri =
resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues)
uri?.let { uri1 ->
resolver.openOutputStream(uri1).use {
((imageView.drawable as BitmapDrawable).bitmap).compress(
Bitmap.CompressFormat.PNG,100,it
)
}
}
outFile = (uri?.path)?.let { File(it) }
} else {
try {
outFile =
File(
getimageDirectory(),"Draw_Easy " + System.currentTimeMillis() + ".png"
)
val outputStream = outFile?.let { FileOutputStream(it) }
((imageView.drawable as BitmapDrawable).bitmap).compress(
Bitmap.CompressFormat.PNG,outputStream
)
outputStream?.flush()
outputStream?.close()
scanFile(outFile!!,"image/png")
} catch (e: java.lang.Exception) {
Log.e("TAG","Failed",e)
}
}
}
Toast.makeText(
this@MainActivity,"Image Saved to ${outFile?.absolutePath}",Toast.LENGTH_LONG
).show()
}
无论在代码中与输出流进行交互时,尽管运行withContext(@R_502[email protected])的“所谓的”阻塞方法调用,它仍会给我“不适当的阻塞方法调用”错误。
编辑:scopeForSaving
的逻辑如下:
private var job = Job() //initialization of job
private var scopeForSaving = Coroutinescope(job + @R_502[email protected])
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)