-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make screenshot works with android 4.4
- Loading branch information
1 parent
ca23637
commit 98232d1
Showing
3 changed files
with
81 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/exec" | ||
|
||
cli "gopkg.in/urfave/cli.v1" | ||
) | ||
|
||
func adbCommand(serial string, args ...string) *exec.Cmd { | ||
c := exec.Command(adbPath(), args...) | ||
c.Env = append(os.Environ(), "ANDROID_SERIAL="+serial) | ||
return c | ||
} | ||
|
||
func screenshotExecOut(serial, output string) error { | ||
serial, err := chooseOne() | ||
if err != nil { | ||
return err | ||
} | ||
c := adbCommand(serial, "exec-out", "screencap", "-p") | ||
imgfile, err := os.Create(output) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
imgfile.Close() | ||
if err != nil { | ||
os.Remove(output) | ||
} | ||
}() | ||
c.Stdout = imgfile | ||
// c.Stderr = os.Stderr | ||
return c.Run() | ||
} | ||
|
||
func screenshotScreencap(serial, output string) error { | ||
tmpPath := "/sdcard/fa-screenshot.png" | ||
c := adbCommand(serial, "shell", "screencap", "-p", tmpPath) | ||
if err := c.Run(); err != nil { | ||
return err | ||
} | ||
defer adbCommand(serial, "shell", "rm", tmpPath).Run() | ||
return adbCommand(serial, "pull", tmpPath, output).Run() | ||
} | ||
|
||
func actScreenshot(ctx *cli.Context) (err error) { | ||
serial, err := chooseOne() | ||
if err != nil { | ||
return err | ||
} | ||
output := ctx.String("output") | ||
err = screenshotExecOut(serial, output) | ||
if err != nil { | ||
// log.Println("FAIL:", "exec-out", "screencap") | ||
err = screenshotScreencap(serial, output) | ||
} | ||
if err == nil { | ||
// log.Println("OKAY:", "shell", "screencap") | ||
log.Println("save to", output) | ||
if ctx.Bool("open") { | ||
// TODO(ssx): only works on mac | ||
exec.Command("open", output).Run() | ||
} | ||
} | ||
return | ||
} |