Chào mọi người em đang xây dựng ứng dụng chụp lại tất cả những thứ hiển thị trên màn hình. Ứng dụng của em sẽ chạy ngầm. Đây là mẫu code chụp lại ảnh màn hình bình thường:
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()
    }
}
Tuy nhiên khi áp dụng vào service chạy ngầm thì gặp lỗi chỗ window trong val v1 = window.decorView.rootView ,liệu có phải khi chạy ở chế độ nền không được hay sao ạ mọi người.
 
      
     83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?
    83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?