1

I would like to help to find a way to take a screenshot in my app. My app is an Overload app and I would like to take a screenshot of the screen where it is.

I'm using the screencap command by always is generated 0b file. I think that it's missing some permission to works very well.

below follow permission requests:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"
        tools:ignore="ProtectedPermissions" />

below follow the code to take a screenshot

fun takeScreenShotADB(badge:String){
        val t = Thread(Runnable{
            val date = Date()
            val formatDate: CharSequence = DateFormat.format("ddMMyyyyhhmmss", date)
            val filename = "${badge}_${formatDate}"
            val path = Environment.getExternalStorageDirectory()
            val command = "/system/bin/screencap -p ${path}/DCIM/Screenshots/${filename}.png"
            //val command = arrayOf("/system/bin/screencap","-p", "/sdcard/DCIM/Screenshots/${filename}.png")
            //val str = arrayOf("/system/bin/screencap", "-p", "/sdcard/out.png")

            try {
                //val processBuilder = ProcessBuilder(command)
                //processBuilder.start()
                //val p2 = Runtime.getRuntime().exec("chmod 777 /sdcard")
                //val result2 = p2.waitFor()
                val p = Runtime.getRuntime().exec(command)
                val scanner = Scanner(p.inputStream)
                while (scanner.hasNext()){
                        scanner.next()
                }
                val result = p.waitFor()
                if(result==0){
                    Log.d("ScreenshotHDL", "Created Ok")
                }
                //val res =  runAdbCommand(command)
            }catch (e: Exception){
                Log.d("ScreenshotHDL", "Erro ao salvar o Screenshot:\n${e.message}")
            }

        }).start()

       }

Always Result returns 1 after waitFor function.

Does someone have any idea to solve that? Thank you so much for your attention and participation.

0 Answers0