android - 获取前台服务Android的根视图
问题描述
我有一个带有前台服务的应用程序,可以从麦克风录制然后处理它,现在我想创建另一个在后台运行的前台服务来捕获 android 屏幕然后还处理捕获的图像,但我不知道该怎么做它。这是我发现的用于执行屏幕捕获工作的应用程序,但是一旦我按下应用程序图标并且我无法启动录制服务,它就会关闭,我在这里找到了同样的问题,这是我的应用程序的应用程序。如果我使用此代码:
private fun takeScreenshot() {
val now = Date()
DateFormat.format("yyyy-MM-dd_hh:mm:ss", now)
val now2= now.toString().split(" ")
try {
// image naming and path to include sd card appending name you choose for file
val mPath: String =
getExternalFilesDir(null).toString().toString() + "/" + now2 + ".png"
Log.d("path",mPath)
// create bitmap screen capture
val v1 = window.decorView.rootView
v1.isDrawingCacheEnabled = true
val bitmap = Bitmap.createBitmap(v1.drawingCache)
v1.isDrawingCacheEnabled = false
val imageFile = File(mPath)
val outputStream = FileOutputStream(imageFile)
val quality = 100
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream)
outputStream.flush()
outputStream.close()
} catch (e: Throwable) {
// Several error may come out with file handling or DOM
e.printStackTrace()
}
}
我无法从前台服务获取 rootview。请帮我
解决方案
推荐阅读
- amazon-web-services - 撤销刷新令牌与在 cognito 中使其无效之间的区别
- python - 部署 Python / Dash Web App 建议时出现 H12 错误
- javascript - cors被阻止无法获取
- mysql - 优化更新速度。减少在每个 API 请求上更新“last_activity”所需的数据库调用
- wordpress - 在 Wordpress 中获取没有明确分类 ID 的自定义分类的条款
- git - 具有多个分支和 CI-CD 的 lerna
- c++ - 什么会导致变量出现在链接器映射文件中而不是 ELF 符号表中?
- java - 从应用程序读取属性时获取不需要的字符。属性文件
- matlab - MatLab 上的值迭代
- python - 重复的 PyQT 引用